@@ -123,7 +123,7 @@ Compat.hasproperty(df::AbstractDataFrame, s::AbstractString) = haskey(index(df),
123123 rename!(df::AbstractDataFrame, (from => to)::Pair...)
124124 rename!(df::AbstractDataFrame, d::AbstractDict)
125125 rename!(df::AbstractDataFrame, d::AbstractVector{<:Pair})
126- rename!(f::Function, df::AbstractDataFrame)
126+ rename!(f::Function, df::AbstractDataFrame; cols=All() )
127127
128128Rename columns of `df` in-place.
129129Each name is changed at most once. Permutation of names is allowed.
@@ -132,8 +132,10 @@ Each name is changed at most once. Permutation of names is allowed.
132132- `df` : the `AbstractDataFrame`
133133- `d` : an `AbstractDict` or an `AbstractVector` of `Pair`s that maps
134134 the original names or column numbers to new names
135- - `f` : a function which for each column takes the old name as a `String`
136- and returns the new name that gets converted to a `Symbol`
135+ - `f` : a function which for each column selected by the `cols` keyword argument
136+ takes the old name as a `String`
137+ and returns the new name that gets converted to a `Symbol`; the `cols`
138+ column selector can be any value accepted as column selector by the `names` function
137139- `vals` : new column names as a vector of `Symbol`s or `AbstractString`s
138140 of the same length as the number of columns in `df`
139141- `makeunique` : if `false` (the default), an error will be raised
@@ -194,6 +196,14 @@ julia> rename!(uppercase, df)
194196 │ Int64 Int64 Int64
195197─────┼─────────────────────
196198 1 │ 1 2 3
199+
200+ julia> rename!(lowercase, df, cols=contains('A'))
201+ 1×3 DataFrame
202+ Row │ a B a_1
203+ │ Int64 Int64 Int64
204+ ─────┼─────────────────────
205+ 1 │ 1 2 3
206+
197207```
198208"""
199209function rename! (df:: AbstractDataFrame , vals:: AbstractVector{Symbol} ;
252262
253263rename! (df:: AbstractDataFrame , args:: Pair... ) = rename! (df, collect (args))
254264
255- function rename! (f:: Function , df:: AbstractDataFrame )
256- rename! (f, index (df))
257- # renaming columns of SubDataFrame has to clean non-note metadata in its parent
258- _drop_all_nonnote_metadata! (parent (df))
259- return df
260- end
265+ rename! (f:: Function , df:: AbstractDataFrame ; cols= All ()) =
266+ rename! (df, [n => Symbol (f (n)) for n in names (df, cols)])
261267
262268"""
263269 rename(df::AbstractDataFrame, vals::AbstractVector{Symbol};
267273 rename(df::AbstractDataFrame, (from => to)::Pair...)
268274 rename(df::AbstractDataFrame, d::AbstractDict)
269275 rename(df::AbstractDataFrame, d::AbstractVector{<:Pair})
270- rename(f::Function, df::AbstractDataFrame)
276+ rename(f::Function, df::AbstractDataFrame; cols=All() )
271277
272278Create a new data frame that is a copy of `df` with changed column names.
273279Each name is changed at most once. Permutation of names is allowed.
@@ -277,8 +283,10 @@ Each name is changed at most once. Permutation of names is allowed.
277283 only allowed if it was created using `:` as a column selector.
278284- `d` : an `AbstractDict` or an `AbstractVector` of `Pair`s that maps
279285 the original names or column numbers to new names
280- - `f` : a function which for each column takes the old name as a `String`
281- and returns the new name that gets converted to a `Symbol`
286+ - `f` : a function which for each column selected by the `cols` keyword argument
287+ takes the old name as a `String`
288+ and returns the new name that gets converted to a `Symbol`; the `cols`
289+ column selector can be any value accepted as column selector by the `names` function
282290- `vals` : new column names as a vector of `Symbol`s or `AbstractString`s
283291 of the same length as the number of columns in `df`
284292- `makeunique` : if `false` (the default), an error will be raised
@@ -350,14 +358,22 @@ julia> rename(uppercase, df)
350358 │ Int64 Int64 Int64
351359─────┼─────────────────────
352360 1 │ 1 2 3
361+
362+ julia> rename(uppercase, df, cols=contains('x'))
363+ 1×3 DataFrame
364+ Row │ i X y
365+ │ Int64 Int64 Int64
366+ ─────┼─────────────────────
367+ 1 │ 1 2 3
368+
353369```
354370"""
355371rename (df:: AbstractDataFrame , vals:: AbstractVector{Symbol} ;
356372 makeunique:: Bool = false ) = rename! (copy (df), vals, makeunique= makeunique)
357373rename (df:: AbstractDataFrame , vals:: AbstractVector{<:AbstractString} ;
358374 makeunique:: Bool = false ) = rename! (copy (df), vals, makeunique= makeunique)
359375rename (df:: AbstractDataFrame , args... ) = rename! (copy (df), args... )
360- rename (f:: Function , df:: AbstractDataFrame ) = rename! (f, copy (df))
376+ rename (f:: Function , df:: AbstractDataFrame ; cols = All ()) = rename! (f, copy (df); cols = cols )
361377
362378"""
363379 size(df::AbstractDataFrame[, dim])
0 commit comments