Skip to content

Commit 4ebddeb

Browse files
committed
Fix DataFrame Merge issue
# Conflicts: # src/Microsoft.Data.Analysis/BooleanDataFrameColumn.cs # src/Microsoft.Data.Analysis/ByteDataFrameColumn.cs # src/Microsoft.Data.Analysis/CharDataFrameColumn.cs # src/Microsoft.Data.Analysis/DateTimeDataFrameColumn.cs # src/Microsoft.Data.Analysis/DecimalDataFrameColumn.cs # src/Microsoft.Data.Analysis/DoubleDataFrameColumn.cs # src/Microsoft.Data.Analysis/Int16DataFrameColumn.cs # src/Microsoft.Data.Analysis/Int32DataFrameColumn.cs # src/Microsoft.Data.Analysis/Int64DataFrameColumn.cs # src/Microsoft.Data.Analysis/SByteDataFrameColumn.cs # src/Microsoft.Data.Analysis/SingleDataFrameColumn.cs # src/Microsoft.Data.Analysis/UInt16DataFrameColumn.cs # src/Microsoft.Data.Analysis/UInt32DataFrameColumn.cs # src/Microsoft.Data.Analysis/UInt64DataFrameColumn.cs # test/Microsoft.Data.Analysis.Tests/DataFrame.IOTests.cs
1 parent 91d0f1f commit 4ebddeb

File tree

3 files changed

+81
-3
lines changed

3 files changed

+81
-3
lines changed

src/Microsoft.Data.Analysis/DataFrameColumnCollection.cs

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -220,6 +220,23 @@ public PrimitiveDataFrameColumn<T> GetPrimitiveColumn<T>(string name)
220220
throw new ArgumentException(string.Format(Strings.BadColumnCast, column.DataType, typeof(T)), nameof(T));
221221
}
222222

223+
/// <summary>
224+
/// Gets the <see cref="PrimitiveDataFrameColumn{T}"/> with the specified <paramref name="name"/>.
225+
/// </summary>
226+
/// <param name="name">The name of the column</param>
227+
/// <returns><see cref="PrimitiveDataFrameColumn{T}"/>.</returns>
228+
/// <exception cref="ArgumentException">A column named <paramref name="name"/> cannot be found, or if the column's type doesn't match.</exception>
229+
public PrimitiveDataFrameColumn<DateTime> GetDateTimeColumn(string name)
230+
{
231+
DataFrameColumn column = this[name];
232+
if (column is PrimitiveDataFrameColumn<DateTime> ret)
233+
{
234+
return ret;
235+
}
236+
237+
throw new ArgumentException(string.Format(Strings.BadColumnCast, column.DataType, typeof(DateTime)));
238+
}
239+
223240
/// <summary>
224241
/// Gets the <see cref="ArrowStringDataFrameColumn"/> with the specified <paramref name="name"/>.
225242
/// </summary>

src/Microsoft.Data.Analysis/PrimitiveDataFrameColumn.cs

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -220,6 +220,16 @@ protected override IReadOnlyList<object> GetValues(long startIndex, int length)
220220
return ret;
221221
}
222222

223+
internal virtual PrimitiveDataFrameColumn<T> CreateNewColumn(string name, PrimitiveColumnContainer<T> container)
224+
{
225+
return new PrimitiveDataFrameColumn<T>(name, container);
226+
}
227+
228+
protected virtual PrimitiveDataFrameColumn<T> CreateNewColumn(string name, long length = 0)
229+
{
230+
return new PrimitiveDataFrameColumn<T>(name, length);
231+
}
232+
223233
internal T? GetTypedValue(long rowIndex) => _columnContainer[rowIndex];
224234

225235
protected override object GetValue(long rowIndex) => GetTypedValue(rowIndex);
@@ -411,7 +421,7 @@ private PrimitiveDataFrameColumn<T> Clone(PrimitiveDataFrameColumn<bool> boolCol
411421
{
412422
if (boolColumn.Length > Length)
413423
throw new ArgumentException(Strings.MapIndicesExceedsColumnLenth, nameof(boolColumn));
414-
PrimitiveDataFrameColumn<T> ret = new PrimitiveDataFrameColumn<T>(Name);
424+
PrimitiveDataFrameColumn<T> ret = CreateNewColumn(Name);
415425
for (long i = 0; i < boolColumn.Length; i++)
416426
{
417427
bool? value = boolColumn[i];
@@ -438,7 +448,8 @@ private PrimitiveDataFrameColumn<T> CloneImplementation<U>(PrimitiveDataFrameCol
438448
}
439449
else
440450
throw new NotImplementedException();
441-
PrimitiveDataFrameColumn<T> ret = new PrimitiveDataFrameColumn<T>(Name, retContainer);
451+
452+
PrimitiveDataFrameColumn<T> ret = CreateNewColumn(Name, retContainer);
442453
return ret;
443454
}
444455

@@ -447,7 +458,7 @@ public PrimitiveDataFrameColumn<T> Clone(PrimitiveDataFrameColumn<long> mapIndic
447458
if (mapIndices is null)
448459
{
449460
PrimitiveColumnContainer<T> newColumnContainer = _columnContainer.Clone();
450-
return new PrimitiveDataFrameColumn<T>(Name, newColumnContainer);
461+
return CreateNewColumn(Name, newColumnContainer);
451462
}
452463
else
453464
{

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

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2766,6 +2766,56 @@ public void TestMerge_Issue5778()
27662766
MatchRowsOnMergedDataFrame(merge, left, right, 1, 1, 0);
27672767
}
27682768

2769+
[Fact]
2770+
//Issue 6127
2771+
public void TestMerge_CorrectColumnTypes()
2772+
{
2773+
DataFrame left = MakeDataFrameWithAllMutableColumnTypes(2, false);
2774+
DataFrame right = MakeDataFrameWithAllMutableColumnTypes(1);
2775+
2776+
DataFrame merge = left.Merge<int>(right, "Int", "Int");
2777+
2778+
Assert.NotNull(merge.Columns.GetBooleanColumn("Bool_left"));
2779+
Assert.NotNull(merge.Columns.GetBooleanColumn("Bool_right"));
2780+
2781+
Assert.NotNull(merge.Columns.GetDecimalColumn("Decimal_left"));
2782+
Assert.NotNull(merge.Columns.GetDecimalColumn("Decimal_right"));
2783+
2784+
Assert.NotNull(merge.Columns.GetSingleColumn("Float_left"));
2785+
Assert.NotNull(merge.Columns.GetSingleColumn("Float_right"));
2786+
2787+
Assert.NotNull(merge.Columns.GetDoubleColumn("Double_left"));
2788+
Assert.NotNull(merge.Columns.GetDoubleColumn("Double_right"));
2789+
2790+
Assert.NotNull(merge.Columns.GetByteColumn("Byte_left"));
2791+
Assert.NotNull(merge.Columns.GetByteColumn("Byte_right"));
2792+
2793+
Assert.NotNull(merge.Columns.GetCharColumn("Char_left"));
2794+
Assert.NotNull(merge.Columns.GetCharColumn("Char_right"));
2795+
2796+
Assert.NotNull(merge.Columns.GetInt16Column("Short_left"));
2797+
Assert.NotNull(merge.Columns.GetInt16Column("Short_right"));
2798+
2799+
Assert.NotNull(merge.Columns.GetUInt16Column("Ushort_left"));
2800+
Assert.NotNull(merge.Columns.GetUInt16Column("Ushort_right"));
2801+
2802+
Assert.NotNull(merge.Columns.GetInt32Column("Int_left"));
2803+
Assert.NotNull(merge.Columns.GetInt32Column("Int_right"));
2804+
2805+
Assert.NotNull(merge.Columns.GetUInt32Column("Uint_left"));
2806+
Assert.NotNull(merge.Columns.GetUInt32Column("Uint_right"));
2807+
2808+
Assert.NotNull(merge.Columns.GetInt64Column("Long_left"));
2809+
Assert.NotNull(merge.Columns.GetInt64Column("Long_right"));
2810+
2811+
Assert.NotNull(merge.Columns.GetUInt64Column("Ulong_left"));
2812+
Assert.NotNull(merge.Columns.GetUInt64Column("Ulong_right"));
2813+
2814+
Assert.NotNull(merge.Columns.GetDateTimeColumn("DateTime_left"));
2815+
Assert.NotNull(merge.Columns.GetDateTimeColumn("DateTime_right"));
2816+
2817+
}
2818+
27692819
[Fact]
27702820
public void TestDescription()
27712821
{

0 commit comments

Comments
 (0)