Skip to content

Commit 15bcaae

Browse files
authored
Improve docstring for names() (#2882)
1 parent facb672 commit 15bcaae

File tree

2 files changed

+71
-3
lines changed

2 files changed

+71
-3
lines changed

docs/src/man/getting_started.md

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -127,8 +127,27 @@ julia> names(df)
127127
"B"
128128
```
129129

130-
To get column names as `Symbol`s use the `propertynames` function:
130+
You can also filter column names by passing a column selector condition as a second argument.
131+
See the [`names`](@ref) docstring for a detailed list of available conditions.
132+
Here we give some selected examples:
133+
134+
```jldoctest dataframe
135+
julia> names(df, r"A") # a regular expression selector
136+
1-element Vector{String}:
137+
"A"
138+
139+
julia> names(df, Int) # a selector using column element type
140+
1-element Vector{String}:
141+
"A"
142+
143+
julia> names(df, Not(:B)) # selector keeping all columns except :B
144+
1-element Vector{String}:
145+
"A"
131146
```
147+
148+
To get column names as `Symbol`s use the `propertynames` function:
149+
150+
```jldoctest dataframe
132151
julia> propertynames(df)
133152
2-element Vector{Symbol}:
134153
:A

src/abstractdataframe/abstractdataframe.jl

Lines changed: 51 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -61,8 +61,12 @@ abstract type AbstractDataFrame end
6161
##############################################################################
6262

6363
"""
64-
names(df::AbstractDataFrame)
65-
names(df::AbstractDataFrame, cols)
64+
names(df::AbstractDataFrame, cols=:)
65+
names(df::DataFrameRow, cols=:)
66+
names(df::GroupedDataFrame, cols=:)
67+
names(df::DataFrameRows, cols=:)
68+
names(df::DataFrameColumns, cols=:)
69+
names(df::GroupKey)
6670
6771
Return a freshly allocated `Vector{String}` of names of columns contained in `df`.
6872
@@ -76,6 +80,51 @@ selector (this is useful in particular with regular expressions, `Cols`, `Not`,
7680
for columns that should be kept
7781
7882
See also [`propertynames`](@ref) which returns a `Vector{Symbol}`.
83+
84+
# Examples
85+
```jldoctest
86+
julia> df = DataFrame(x1=[1, missing, missing], x2=[3, 2, 4], x3=[3, missing, 2], x4=Union{Int, Missing}[2, 4, 4])
87+
3×4 DataFrame
88+
Row │ x1 x2 x3 x4
89+
│ Int64? Int64 Int64? Int64?
90+
─────┼─────────────────────────────────
91+
1 │ 1 3 3 2
92+
2 │ missing 2 missing 4
93+
3 │ missing 4 2 4
94+
95+
julia> names(df)
96+
4-element Vector{String}:
97+
"x1"
98+
"x2"
99+
"x3"
100+
"x4"
101+
102+
julia> names(df, Int) # pick columns whose element type is Int
103+
1-element Vector{String}:
104+
"x2"
105+
106+
julia> names(df, x -> x[end] == '2') # pick columns for which last character in their name is '2'
107+
1-element Vector{String}:
108+
"x2"
109+
110+
julia> fun(col) = sum(skipmissing(col)) >= 10
111+
fun (generic function with 1 method)
112+
113+
julia> names(df, fun.(eachcol(df))) # pick columns for which sum of their elements is at least 10
114+
1-element Vector{String}:
115+
"x4"
116+
117+
julia> names(df, eltype.(eachcol(df)) .>: Missing) # pick columns that allow missing values
118+
3-element Vector{String}:
119+
"x1"
120+
"x3"
121+
"x4"
122+
123+
julia> names(df, any.(ismissing, eachcol(df))) # pick columns that contain missing values
124+
2-element Vector{String}:
125+
"x1"
126+
"x3"
127+
```
79128
"""
80129
Base.names(df::AbstractDataFrame, cols::Colon=:) = names(index(df))
81130

0 commit comments

Comments
 (0)