Skip to content

Fix issue with addIndexColumn in DataFrame.LoadCsv #6769

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Aug 25, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 3 additions & 4 deletions src/Microsoft.Data.Analysis/DataFrame.IO.cs
Original file line number Diff line number Diff line change
Expand Up @@ -439,14 +439,13 @@ private static DataFrame ReadCsvLinesIntoDataFrame(WrappedStreamReaderOrStringRe

if (addIndexColumn)
{
PrimitiveDataFrameColumn<int> indexColumn = new PrimitiveDataFrameColumn<int>("IndexColumn", columns[0].Length);
for (int i = 0; i < columns[0].Length; i++)
Int64DataFrameColumn indexColumn = new Int64DataFrameColumn("IndexColumn", columns[0].Length);
for (long i = 0; i < columns[0].Length; i++)
{
indexColumn[i] = i;
}
columns.Insert(0, indexColumn);
ret.Columns.Insert(0, indexColumn);
}

}

return ret;
Expand Down
13 changes: 13 additions & 0 deletions test/Microsoft.Data.Analysis.Tests/DataFrame.IOTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -622,6 +622,19 @@ public void TestReadCsvWithRepeatColumnNameInHeader()
Assert.Matches(@"DataFrame already contains a column called Column( \(Parameter 'column'\)|\r\nParameter name: column)", exp.Message);
}

[Fact]
public void TestLoadCsvWithAddIndexColumn()
{
var dataFrame = DataFrame.LoadCsvFromString("11\r\n22\r\n33", header: false, addIndexColumn: true);

Assert.Equal(2, dataFrame.Columns.Count);
Assert.Equal("IndexColumn", dataFrame.Columns[0].Name);
Assert.Equal(3, dataFrame.Columns[0].Length);

for (long i = 0; i < dataFrame.Columns[0].Length; i++)
Assert.Equal(i, dataFrame.Columns[0][i]);
}

[Fact]
public void TestReadCsvWithExtraColumnInRow()
{
Expand Down