Skip to content

Commit 7fcb93b

Browse files
authored
avoid not specialized Pair issue (#2889)
1 parent 24bc695 commit 7fcb93b

File tree

4 files changed

+25
-4
lines changed

4 files changed

+25
-4
lines changed

NEWS.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,8 @@
7777

7878
* fix a problem with `unstack` on empty data frame
7979
([#2842](https://github.com/JuliaData/DataFrames.jl/issues/2842))
80+
* fix a problem with not specialized `Pair` arguments passed as transformations
81+
([#2889](https://github.com/JuliaData/DataFrames.jl/issues/2889))
8082

8183
# DataFrames.jl v1.2.2 Patch Release Notes
8284

src/abstractdataframe/selection.jl

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -182,6 +182,10 @@ end
182182
# add a method to funname defined in other/utils.jl
183183
funname(row::ByRow) = funname(row.fun)
184184

185+
make_pair_concrete(@nospecialize(x::Pair)) =
186+
make_pair_concrete(x.first) => make_pair_concrete(x.second)
187+
make_pair_concrete(@nospecialize(x)) = x
188+
185189
normalize_selection(idx::AbstractIndex, @nospecialize(sel), renamecols::Bool) =
186190
try
187191
idx[sel]
@@ -1328,7 +1332,7 @@ function manipulate(df::DataFrame, @nospecialize(cs...); copycols::Bool, keeprow
13281332
push!(cs_vec, v)
13291333
end
13301334
end
1331-
return _manipulate(df, Any[normalize_selection(index(df), c, renamecols) for c in cs_vec],
1335+
return _manipulate(df, Any[normalize_selection(index(df), make_pair_concrete(c), renamecols) for c in cs_vec],
13321336
copycols, keeprows)
13331337
end
13341338

@@ -1418,7 +1422,8 @@ function manipulate(dfv::SubDataFrame, @nospecialize(args...); copycols::Bool, k
14181422
push!(cs_vec, v)
14191423
end
14201424
end
1421-
return _manipulate(dfv, Any[normalize_selection(index(dfv), c, renamecols) for c in cs_vec],
1425+
return _manipulate(dfv, Any[normalize_selection(index(dfv),
1426+
make_pair_concrete(c), renamecols) for c in cs_vec],
14221427
true, keeprows)
14231428
else
14241429
# we do not support transformations here
@@ -1436,7 +1441,7 @@ function manipulate(dfv::SubDataFrame, @nospecialize(args...); copycols::Bool, k
14361441
push!(seen_single_column, ind_idx)
14371442
end
14381443
else
1439-
newind = normalize_selection(index(dfv), ind, renamecols)
1444+
newind = normalize_selection(index(dfv), make_pair_concrete(ind), renamecols)
14401445
if newind isa Pair
14411446
throw(ArgumentError("transforming and renaming columns of a " *
14421447
"SubDataFrame is not allowed when `copycols=false`"))

src/groupeddataframe/splitapplycombine.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ function _combine_prepare_norm(gd::GroupedDataFrame,
6262
cs_norm = []
6363
optional_transform = Bool[]
6464
for c in cs_vec
65-
arg = normalize_selection(index(parent(gd)), c, renamecols)
65+
arg = normalize_selection(index(parent(gd)), make_pair_concrete(c), renamecols)
6666
if arg isa AbstractVector{Int}
6767
for col_idx in arg
6868
push!(cs_norm, col_idx => identity => _names(gd)[col_idx])

test/select.jl

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1678,4 +1678,18 @@ end
16781678
@test select(df, :a => ByRow(f) => :a) == DataFrame(a=1:3)
16791679
end
16801680

1681+
@testset "vectors of pairs with non-specific type are accepted" begin
1682+
df = DataFrame(x=[1,2,3])
1683+
@test combine(df, [1 => length => :a, 1 => length => "b"]) == DataFrame(a=3, b=3)
1684+
@test combine(df, [:x => length => :a, 1 => :b]) == DataFrame(a=3, b=1:3)
1685+
gdf = groupby(df, :x)
1686+
@test combine(gdf, [1 => length => :a, 1 => length => "b"]) == DataFrame(x=1:3, a=1, b=1)
1687+
@test combine(gdf, [:x => length => :a, 1 => :b]) == DataFrame(x=1:3, a=1, b=1:3)
1688+
sdf = view(df, :, :)
1689+
@test select(sdf, [1 => length => :a, 1 => length => "b"]) == DataFrame(a=[3, 3, 3], b=[3, 3, 3])
1690+
@test select(sdf, [:x => length => :a, 1 => :b]) == DataFrame(a=3, b=1:3)
1691+
@test_throws ArgumentError select(sdf, [1 => length => :a, 1 => length => "b"], copycols=false)
1692+
@test_throws ArgumentError select(sdf, [:x => length => :a, 1 => :b], copycols=false)
1693+
end
1694+
16811695
end # module

0 commit comments

Comments
 (0)