Skip to content

Commit 53c0f26

Browse files
Fix the behavior or column SetName method (#6676)
* Fix the behavior or column SetName method * Fix stack overflow exception * Fix merge issues --------- Co-authored-by: Michael Sharp <[email protected]>
1 parent 443ceb9 commit 53c0f26

File tree

5 files changed

+96
-13
lines changed

5 files changed

+96
-13
lines changed

src/Microsoft.Data.Analysis/DataFrame.Join.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ private void SetSuffixForDuplicatedColumnNames(DataFrame dataFrame, DataFrameCol
3030
{
3131
// Pre-existing column. Change name
3232
DataFrameColumn existingColumn = dataFrame.Columns[index];
33-
dataFrame._columnCollection.SetColumnName(existingColumn, existingColumn.Name + leftSuffix);
33+
existingColumn.SetName(existingColumn.Name + leftSuffix);
3434
column.SetName(column.Name + rightSuffix);
3535
index = dataFrame._columnCollection.IndexOf(column.Name);
3636
}

src/Microsoft.Data.Analysis/DataFrame.cs

+2-2
Original file line numberDiff line numberDiff line change
@@ -301,7 +301,7 @@ public DataFrame AddPrefix(string prefix, bool inPlace = false)
301301
for (int i = 0; i < df.Columns.Count; i++)
302302
{
303303
DataFrameColumn column = df.Columns[i];
304-
df._columnCollection.SetColumnName(column, prefix + column.Name);
304+
column.SetName(prefix + column.Name);
305305
df.OnColumnsChanged();
306306
}
307307
return df;
@@ -316,7 +316,7 @@ public DataFrame AddSuffix(string suffix, bool inPlace = false)
316316
for (int i = 0; i < df.Columns.Count; i++)
317317
{
318318
DataFrameColumn column = df.Columns[i];
319-
df._columnCollection.SetColumnName(column, column.Name + suffix);
319+
column.SetName(column.Name + suffix);
320320
df.OnColumnsChanged();
321321
}
322322
return df;

src/Microsoft.Data.Analysis/DataFrameColumn.cs

+34-8
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,26 @@ protected set
8484
}
8585
}
8686

87+
// List of ColumnCollections that owns the column
88+
// Current API allows column to be added into multiple dataframes, that's why the list is needed
89+
private readonly List<DataFrameColumnCollection> _ownerColumnCollections = new();
90+
91+
internal void AddOwner(DataFrameColumnCollection columCollection)
92+
{
93+
if (!_ownerColumnCollections.Contains(columCollection))
94+
{
95+
_ownerColumnCollections.Add(columCollection);
96+
}
97+
}
98+
99+
internal void RemoveOwner(DataFrameColumnCollection columCollection)
100+
{
101+
if (_ownerColumnCollections.Contains(columCollection))
102+
{
103+
_ownerColumnCollections.Remove(columCollection);
104+
}
105+
}
106+
87107
/// <summary>
88108
/// The number of <see langword="null" /> values in this column.
89109
/// </summary>
@@ -95,24 +115,30 @@ public abstract long NullCount
95115
private string _name;
96116

97117
/// <summary>
98-
/// The name of this column.
118+
/// The column name.
99119
/// </summary>
100120
public string Name => _name;
101121

102122
/// <summary>
103-
/// Updates the name of this column.
123+
/// Updates the column name.
104124
/// </summary>
105125
/// <param name="newName">The new name.</param>
106-
/// <param name="dataFrame">If passed in, update the column name in <see cref="DataFrame.Columns"/></param>
107-
public void SetName(string newName, DataFrame dataFrame = null)
126+
public void SetName(string newName)
108127
{
109-
if (!(dataFrame is null))
110-
{
111-
dataFrame.Columns.SetColumnName(this, newName);
112-
}
128+
foreach (var owner in _ownerColumnCollections)
129+
owner.UpdateColumnNameMetadata(this, newName);
130+
113131
_name = newName;
114132
}
115133

134+
/// <summary>
135+
/// Updates the name of this column.
136+
/// </summary>
137+
/// <param name="newName">The new name.</param>
138+
/// <param name="dataFrame">Ignored (for backward compatibility)</param>
139+
[Obsolete]
140+
public void SetName(string newName, DataFrame dataFrame) => SetName(newName);
141+
116142
/// <summary>
117143
/// The type of data this column holds.
118144
/// </summary>

src/Microsoft.Data.Analysis/DataFrameColumnCollection.cs

+21-2
Original file line numberDiff line numberDiff line change
@@ -38,11 +38,23 @@ internal IReadOnlyList<string> GetColumnNames()
3838
return ret;
3939
}
4040

41+
public void RenameColumn(string currentName, string newName)
42+
{
43+
var column = this[currentName];
44+
column.SetName(newName);
45+
}
46+
47+
[Obsolete]
4148
public void SetColumnName(DataFrameColumn column, string newName)
49+
{
50+
column.SetName(newName);
51+
}
52+
53+
//Updates column's metadata (is used as a callback from Column class)
54+
internal void UpdateColumnNameMetadata(DataFrameColumn column, string newName)
4255
{
4356
string currentName = column.Name;
4457
int currentIndex = _columnNameToIndexDictionary[currentName];
45-
column.SetName(newName);
4658
_columnNameToIndexDictionary.Remove(currentName);
4759
_columnNameToIndexDictionary.Add(newName, currentIndex);
4860
ColumnsChanged?.Invoke();
@@ -75,6 +87,8 @@ protected override void InsertItem(int columnIndex, DataFrameColumn column)
7587
throw new ArgumentException(string.Format(Strings.DuplicateColumnName, column.Name), nameof(column));
7688
}
7789

90+
column.AddOwner(this);
91+
7892
RowCount = column.Length;
7993

8094
_columnNameToIndexDictionary[column.Name] = columnIndex;
@@ -98,9 +112,13 @@ protected override void SetItem(int columnIndex, DataFrameColumn column)
98112
{
99113
throw new ArgumentException(string.Format(Strings.DuplicateColumnName, column.Name), nameof(column));
100114
}
115+
101116
_columnNameToIndexDictionary.Remove(this[columnIndex].Name);
102117
_columnNameToIndexDictionary[column.Name] = columnIndex;
118+
119+
this[columnIndex].RemoveOwner(this);
103120
base.SetItem(columnIndex, column);
121+
104122
ColumnsChanged?.Invoke();
105123
}
106124

@@ -111,6 +129,8 @@ protected override void RemoveItem(int columnIndex)
111129
{
112130
_columnNameToIndexDictionary[this[i].Name]--;
113131
}
132+
133+
this[columnIndex].RemoveOwner(this);
114134
base.RemoveItem(columnIndex);
115135

116136
//Reset RowCount if the last column was removed and dataframe is empty
@@ -474,6 +494,5 @@ public UInt16DataFrameColumn GetUInt16Column(string name)
474494

475495
throw new ArgumentException(string.Format(Strings.BadColumnCast, column.DataType, typeof(UInt16)));
476496
}
477-
478497
}
479498
}

test/Microsoft.Data.Analysis.Tests/DataFrameTests.cs

+38
Original file line numberDiff line numberDiff line change
@@ -388,6 +388,44 @@ public void ClearColumnsTests()
388388
Assert.Equal(0, dataFrame.Columns.LongCount());
389389
}
390390

391+
[Fact]
392+
public void RenameColumnWithSetNameTests()
393+
{
394+
StringDataFrameColumn city = new StringDataFrameColumn("City", new string[] { "London", "Berlin" });
395+
PrimitiveDataFrameColumn<int> temp = new PrimitiveDataFrameColumn<int>("Temperature", new int[] { 12, 13 });
396+
397+
DataFrame dataframe = new DataFrame(city, temp);
398+
399+
// Change the name of the column:
400+
dataframe["City"].SetName("Town");
401+
var renamedColumn = dataframe["Town"];
402+
403+
Assert.Throws<ArgumentException>(() => dataframe["City"]);
404+
405+
Assert.NotNull(renamedColumn);
406+
Assert.Equal("Town", renamedColumn.Name);
407+
Assert.True(ReferenceEquals(city, renamedColumn));
408+
}
409+
410+
[Fact]
411+
public void RenameColumnWithRenameColumnTests()
412+
{
413+
StringDataFrameColumn city = new StringDataFrameColumn("City", new string[] { "London", "Berlin" });
414+
PrimitiveDataFrameColumn<int> temp = new PrimitiveDataFrameColumn<int>("Temperature", new int[] { 12, 13 });
415+
416+
DataFrame dataframe = new DataFrame(city, temp);
417+
418+
// Change the name of the column:
419+
dataframe.Columns.RenameColumn("City", "Town");
420+
var renamedColumn = dataframe["Town"];
421+
422+
Assert.Throws<ArgumentException>(() => dataframe["City"]);
423+
424+
Assert.NotNull(renamedColumn);
425+
Assert.Equal("Town", renamedColumn.Name);
426+
Assert.True(ReferenceEquals(city, renamedColumn));
427+
}
428+
391429
[Fact]
392430
public void TestBinaryOperations()
393431
{

0 commit comments

Comments
 (0)