-
Notifications
You must be signed in to change notification settings - Fork 171
/
Copy pathdataframe.py
434 lines (400 loc) · 21.6 KB
/
dataframe.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
from __future__ import annotations
from polars.type_aliases import ColumnNameOrSelector
from janitor.utils import import_message
from .clean_names import _clean_column_names
from .pivot_longer import _pivot_longer
from .row_to_names import _row_to_names
try:
import polars as pl
except ImportError:
import_message(
submodule="polars",
package="polars",
conda_channel="conda-forge",
pip_install=True,
)
@pl.api.register_dataframe_namespace("janitor")
class PolarsDataFrame:
def __init__(self, df: pl.DataFrame) -> pl.DataFrame:
self._df = df
def clean_names(
self,
strip_underscores: str | bool = None,
case_type: str = "lower",
remove_special: bool = False,
strip_accents: bool = False,
truncate_limit: int = None,
) -> pl.DataFrame:
"""
Clean the column names in a polars DataFrame.
Examples:
>>> import polars as pl
>>> import janitor.polars
>>> df = pl.DataFrame(
... {
... "Aloha": range(3),
... "Bell Chart": range(3),
... "Animals@#$%^": range(3)
... }
... )
>>> df
shape: (3, 3)
┌───────┬────────────┬──────────────┐
│ Aloha ┆ Bell Chart ┆ Animals@#$%^ │
│ --- ┆ --- ┆ --- │
│ i64 ┆ i64 ┆ i64 │
╞═══════╪════════════╪══════════════╡
│ 0 ┆ 0 ┆ 0 │
│ 1 ┆ 1 ┆ 1 │
│ 2 ┆ 2 ┆ 2 │
└───────┴────────────┴──────────────┘
>>> df.janitor.clean_names(remove_special=True)
shape: (3, 3)
┌───────┬────────────┬─────────┐
│ aloha ┆ bell_chart ┆ animals │
│ --- ┆ --- ┆ --- │
│ i64 ┆ i64 ┆ i64 │
╞═══════╪════════════╪═════════╡
│ 0 ┆ 0 ┆ 0 │
│ 1 ┆ 1 ┆ 1 │
│ 2 ┆ 2 ┆ 2 │
└───────┴────────────┴─────────┘
!!! info "New in version 0.28.0"
Args:
strip_underscores: Removes the outer underscores from all
column names. Default None keeps outer underscores. Values can be
either 'left', 'right' or 'both' or the respective shorthand 'l',
'r' and True.
case_type: Whether to make the column names lower or uppercase.
Current case may be preserved with 'preserve',
while snake case conversion (from CamelCase or camelCase only)
can be turned on using "snake".
Default 'lower' makes all characters lowercase.
remove_special: Remove special characters from the column names.
Only letters, numbers and underscores are preserved.
strip_accents: Whether or not to remove accents from
the labels.
truncate_limit: Truncates formatted column names to
the specified length. Default None does not truncate.
Returns:
A polars DataFrame.
""" # noqa: E501
return self._df.rename(
lambda col: _clean_column_names(
obj=col,
strip_accents=strip_accents,
strip_underscores=strip_underscores,
case_type=case_type,
remove_special=remove_special,
truncate_limit=truncate_limit,
)
)
def pivot_longer(
self,
index: ColumnNameOrSelector = None,
column_names: ColumnNameOrSelector = None,
names_to: list | tuple | str = "variable",
values_to: str = "value",
names_sep: str = None,
names_pattern: str = None,
names_transform: pl.Expr = None,
) -> pl.DataFrame:
"""
Unpivots a DataFrame from *wide* to *long* format.
It is modeled after the `pivot_longer` function in R's tidyr package,
and also takes inspiration from the `melt` function in R's data.table package.
This function is useful to massage a DataFrame into a format where
one or more columns are considered measured variables, and all other
columns are considered as identifier variables.
All measured variables are *unpivoted* (and typically duplicated) along the
row axis.
For more granular control on the unpivoting, have a look at
`pivot_longer_spec`.
Examples:
>>> import polars as pl
>>> import polars.selectors as cs
>>> import janitor.polars
>>> df = pl.DataFrame(
... {
... "Sepal.Length": [5.1, 5.9],
... "Sepal.Width": [3.5, 3.0],
... "Petal.Length": [1.4, 5.1],
... "Petal.Width": [0.2, 1.8],
... "Species": ["setosa", "virginica"],
... }
... )
>>> df
shape: (2, 5)
┌──────────────┬─────────────┬──────────────┬─────────────┬───────────┐
│ Sepal.Length ┆ Sepal.Width ┆ Petal.Length ┆ Petal.Width ┆ Species │
│ --- ┆ --- ┆ --- ┆ --- ┆ --- │
│ f64 ┆ f64 ┆ f64 ┆ f64 ┆ str │
╞══════════════╪═════════════╪══════════════╪═════════════╪═══════════╡
│ 5.1 ┆ 3.5 ┆ 1.4 ┆ 0.2 ┆ setosa │
│ 5.9 ┆ 3.0 ┆ 5.1 ┆ 1.8 ┆ virginica │
└──────────────┴─────────────┴──────────────┴─────────────┴───────────┘
Replicate polars' [melt](https://docs.pola.rs/py-polars/html/reference/dataframe/api/polars.DataFrame.melt.html#polars-dataframe-melt):
>>> df.janitor.pivot_longer(index = 'Species')
shape: (8, 3)
┌───────────┬──────────────┬───────┐
│ Species ┆ variable ┆ value │
│ --- ┆ --- ┆ --- │
│ str ┆ str ┆ f64 │
╞═══════════╪══════════════╪═══════╡
│ setosa ┆ Sepal.Length ┆ 5.1 │
│ virginica ┆ Sepal.Length ┆ 5.9 │
│ setosa ┆ Sepal.Width ┆ 3.5 │
│ virginica ┆ Sepal.Width ┆ 3.0 │
│ setosa ┆ Petal.Length ┆ 1.4 │
│ virginica ┆ Petal.Length ┆ 5.1 │
│ setosa ┆ Petal.Width ┆ 0.2 │
│ virginica ┆ Petal.Width ┆ 1.8 │
└───────────┴──────────────┴───────┘
Split the column labels into individual columns:
>>> df.janitor.pivot_longer(
... index = 'Species',
... names_to = ('part', 'dimension'),
... names_sep = '.',
... ).select('Species','part','dimension','value')
shape: (8, 4)
┌───────────┬───────┬───────────┬───────┐
│ Species ┆ part ┆ dimension ┆ value │
│ --- ┆ --- ┆ --- ┆ --- │
│ str ┆ str ┆ str ┆ f64 │
╞═══════════╪═══════╪═══════════╪═══════╡
│ setosa ┆ Sepal ┆ Length ┆ 5.1 │
│ virginica ┆ Sepal ┆ Length ┆ 5.9 │
│ setosa ┆ Sepal ┆ Width ┆ 3.5 │
│ virginica ┆ Sepal ┆ Width ┆ 3.0 │
│ setosa ┆ Petal ┆ Length ┆ 1.4 │
│ virginica ┆ Petal ┆ Length ┆ 5.1 │
│ setosa ┆ Petal ┆ Width ┆ 0.2 │
│ virginica ┆ Petal ┆ Width ┆ 1.8 │
└───────────┴───────┴───────────┴───────┘
Retain parts of the column names as headers:
>>> df.janitor.pivot_longer(
... index = 'Species',
... names_to = ('part', '.value'),
... names_sep = '.',
... ).select('Species','part','Length','Width')
shape: (4, 4)
┌───────────┬───────┬────────┬───────┐
│ Species ┆ part ┆ Length ┆ Width │
│ --- ┆ --- ┆ --- ┆ --- │
│ str ┆ str ┆ f64 ┆ f64 │
╞═══════════╪═══════╪════════╪═══════╡
│ setosa ┆ Sepal ┆ 5.1 ┆ 3.5 │
│ virginica ┆ Sepal ┆ 5.9 ┆ 3.0 │
│ setosa ┆ Petal ┆ 1.4 ┆ 0.2 │
│ virginica ┆ Petal ┆ 5.1 ┆ 1.8 │
└───────────┴───────┴────────┴───────┘
Split the column labels based on regex:
>>> df = pl.DataFrame({"id": [1], "new_sp_m5564": [2], "newrel_f65": [3]})
>>> df
shape: (1, 3)
┌─────┬──────────────┬────────────┐
│ id ┆ new_sp_m5564 ┆ newrel_f65 │
│ --- ┆ --- ┆ --- │
│ i64 ┆ i64 ┆ i64 │
╞═════╪══════════════╪════════════╡
│ 1 ┆ 2 ┆ 3 │
└─────┴──────────────┴────────────┘
>>> df.janitor.pivot_longer(
... index = 'id',
... names_to = ('diagnosis', 'gender', 'age'),
... names_pattern = r"new_?(.+)_(.)([0-9]+)",
... ).select('id','diagnosis','gender','age','value').sort(by=pl.all())
shape: (2, 5)
┌─────┬───────────┬────────┬──────┬───────┐
│ id ┆ diagnosis ┆ gender ┆ age ┆ value │
│ --- ┆ --- ┆ --- ┆ --- ┆ --- │
│ i64 ┆ str ┆ str ┆ str ┆ i64 │
╞═════╪═══════════╪════════╪══════╪═══════╡
│ 1 ┆ rel ┆ f ┆ 65 ┆ 3 │
│ 1 ┆ sp ┆ m ┆ 5564 ┆ 2 │
└─────┴───────────┴────────┴──────┴───────┘
Convert the dtypes of specific columns with `names_transform`:
>>> df.janitor.pivot_longer(
... index = "id",
... names_pattern=r"new_?(.+)_(.)([0-9]+)",
... names_to=("diagnosis", "gender", "age"),
... names_transform=pl.col('age').cast(pl.Int32),
... ).select("id", "diagnosis", "gender", "age", "value").sort(by=pl.all())
shape: (2, 5)
┌─────┬───────────┬────────┬──────┬───────┐
│ id ┆ diagnosis ┆ gender ┆ age ┆ value │
│ --- ┆ --- ┆ --- ┆ --- ┆ --- │
│ i64 ┆ str ┆ str ┆ i32 ┆ i64 │
╞═════╪═══════════╪════════╪══════╪═══════╡
│ 1 ┆ rel ┆ f ┆ 65 ┆ 3 │
│ 1 ┆ sp ┆ m ┆ 5564 ┆ 2 │
└─────┴───────────┴────────┴──────┴───────┘
Use multiple `.value` to reshape the dataframe:
>>> df = pl.DataFrame(
... [
... {
... "x_1_mean": 10,
... "x_2_mean": 20,
... "y_1_mean": 30,
... "y_2_mean": 40,
... "unit": 50,
... }
... ]
... )
>>> df
shape: (1, 5)
┌──────────┬──────────┬──────────┬──────────┬──────┐
│ x_1_mean ┆ x_2_mean ┆ y_1_mean ┆ y_2_mean ┆ unit │
│ --- ┆ --- ┆ --- ┆ --- ┆ --- │
│ i64 ┆ i64 ┆ i64 ┆ i64 ┆ i64 │
╞══════════╪══════════╪══════════╪══════════╪══════╡
│ 10 ┆ 20 ┆ 30 ┆ 40 ┆ 50 │
└──────────┴──────────┴──────────┴──────────┴──────┘
>>> df.janitor.pivot_longer(
... index="unit",
... names_to=(".value", "time", ".value"),
... names_pattern=r"(x|y)_([0-9])(_mean)",
... ).select('unit','time','x_mean','y_mean').sort(by=pl.all())
shape: (2, 4)
┌──────┬──────┬────────┬────────┐
│ unit ┆ time ┆ x_mean ┆ y_mean │
│ --- ┆ --- ┆ --- ┆ --- │
│ i64 ┆ str ┆ i64 ┆ i64 │
╞══════╪══════╪════════╪════════╡
│ 50 ┆ 1 ┆ 10 ┆ 30 │
│ 50 ┆ 2 ┆ 20 ┆ 40 │
└──────┴──────┴────────┴────────┘
!!! info "New in version 0.28.0"
Args:
index: Column(s) or selector(s) to use as identifier variables.
column_names: Column(s) or selector(s) to unpivot.
names_to: Name of new column as a string that will contain
what were previously the column names in `column_names`.
The default is `variable` if no value is provided. It can
also be a list/tuple of strings that will serve as new column
names, if `name_sep` or `names_pattern` is provided.
If `.value` is in `names_to`, new column names will be extracted
from part of the existing column names and overrides `values_to`.
values_to: Name of new column as a string that will contain what
were previously the values of the columns in `column_names`.
names_sep: Determines how the column name is broken up, if
`names_to` contains multiple values. It takes the same
specification as polars' `str.split` method.
names_pattern: Determines how the column name is broken up.
It can be a regular expression containing matching groups.
It takes the same
specification as polars' `str.extract_groups` method.
names_transform: Use this option to change the types of columns that
have been transformed to rows.
This does not applies to the values' columns.
Accepts a polars expression or a list of polars expressions.
Applicable only if one of names_sep
or names_pattern is provided.
Returns:
A polars DataFrame that has been unpivoted from wide to long
format.
""" # noqa: E501
return _pivot_longer(
df=self._df,
index=index,
column_names=column_names,
names_pattern=names_pattern,
names_sep=names_sep,
names_to=names_to,
values_to=values_to,
names_transform=names_transform,
)
def row_to_names(
self,
row_numbers: int | list = 0,
remove_rows: bool = False,
remove_rows_above: bool = False,
separator: str = "_",
) -> pl.DataFrame:
"""
Elevates a row, or rows, to be the column names of a DataFrame.
Examples:
Replace column names with the first row.
>>> import polars as pl
>>> import janitor.polars
>>> df = pl.DataFrame({
... "a": ["nums", '6', '9'],
... "b": ["chars", "x", "y"],
... })
>>> df
shape: (3, 2)
┌──────┬───────┐
│ a ┆ b │
│ --- ┆ --- │
│ str ┆ str │
╞══════╪═══════╡
│ nums ┆ chars │
│ 6 ┆ x │
│ 9 ┆ y │
└──────┴───────┘
>>> df.janitor.row_to_names(0, remove_rows=True)
shape: (2, 2)
┌──────┬───────┐
│ nums ┆ chars │
│ --- ┆ --- │
│ str ┆ str │
╞══════╪═══════╡
│ 6 ┆ x │
│ 9 ┆ y │
└──────┴───────┘
>>> df.janitor.row_to_names(row_numbers=[0,1], remove_rows=True)
shape: (1, 2)
┌────────┬─────────┐
│ nums_6 ┆ chars_x │
│ --- ┆ --- │
│ str ┆ str │
╞════════╪═════════╡
│ 9 ┆ y │
└────────┴─────────┘
Remove rows above the elevated row and the elevated row itself.
>>> df = pl.DataFrame({
... "a": ["bla1", "nums", '6', '9'],
... "b": ["bla2", "chars", "x", "y"],
... })
>>> df
shape: (4, 2)
┌──────┬───────┐
│ a ┆ b │
│ --- ┆ --- │
│ str ┆ str │
╞══════╪═══════╡
│ bla1 ┆ bla2 │
│ nums ┆ chars │
│ 6 ┆ x │
│ 9 ┆ y │
└──────┴───────┘
>>> df.janitor.row_to_names(1, remove_rows=True, remove_rows_above=True)
shape: (2, 2)
┌──────┬───────┐
│ nums ┆ chars │
│ --- ┆ --- │
│ str ┆ str │
╞══════╪═══════╡
│ 6 ┆ x │
│ 9 ┆ y │
└──────┴───────┘
!!! info "New in version 0.28.0"
Args:
row_numbers: Position of the row(s) containing the variable names.
Note that indexing starts from 0. It can also be a list.
Defaults to 0 (first row).
remove_rows: Whether the row(s) should be removed from the DataFrame.
remove_rows_above: Whether the row(s) above the selected row should
be removed from the DataFrame.
separator: Combines the labels into a single string,
if row_numbers is a list of integers. Default is '_'.
Returns:
A polars DataFrame.
""" # noqa: E501
return _row_to_names(
self._df,
row_numbers=row_numbers,
remove_rows=remove_rows,
remove_rows_above=remove_rows_above,
separator=separator,
)