Skip to content

Commit

Permalink
Merge pull request #276 from markfairbanks/print-method
Browse files Browse the repository at this point in the history
Add `.print()`
  • Loading branch information
markfairbanks authored Oct 26, 2024
2 parents 2224255 + 7aa1457 commit 7378501
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 41 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@
* `if_else()` treats string inputs in `true` and `false` as strings and not as columns
* `case_when()` no has syntax closer to `dplyr::case_when()`

#### New tibble methods

* `.print()`

## v0.3.0

* Major refactor to work with `polars>=1.0.0`
Expand Down
87 changes: 46 additions & 41 deletions tidypolars/tibble_df.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,14 +37,16 @@ def __init__(self, _data = None, **kwargs):

def __dir__(self):
_tidypolars_methods = [
'arrange', 'bind_cols', 'bind_rows', 'colnames', 'clone', 'count',
'arrange', 'as_dict', 'as_pandas', 'as_polars',
'bind_cols', 'bind_rows', 'colnames', 'clone', 'count',
'distinct', 'drop', 'drop_null', 'head', 'fill', 'filter',
'inner_join', 'left_join', 'mutate', 'names', 'nrow', 'ncol',
'full_join', 'pivot_longer', 'pivot_wider',
'print',
'pull', 'relocate', 'rename', 'replace_null', 'select',
'separate', 'set_names',
'slice', 'slice_head', 'slice_tail', 'summarize', 'tail',
'as_pandas', 'as_polars', 'write_csv', 'write_parquet'
'write_csv', 'write_parquet'
]
return _tidypolars_methods

Expand Down Expand Up @@ -107,6 +109,45 @@ def arrange(self, *args):
exprs = _as_list(args)
desc = [True if isinstance(expr, DescCol) else False for expr in exprs]
return super().sort(exprs, descending = desc).pipe(from_polars)

def as_dict(self, *, as_series = True):
"""
Aggregate data with summary statistics
Parameters
----------
as_series : bool
If True - returns the dict values as Series
If False - returns the dict values as lists
Examples
--------
>>> df.to_dict()
>>> df.to_dict(as_series = False)
"""
return super().to_dict(as_series = as_series)

def as_pandas(self):
"""
Convert to a pandas DataFrame
Examples
--------
>>> df.as_pandas()
"""
return self.as_polars().to_pandas()

def as_polars(self):
"""
Convert to a polars DataFrame
Examples
--------
>>> df.as_polars()
"""
self = copy.copy(self)
self.__class__ = pl.DataFrame
return self

def bind_cols(self, *args):
"""
Expand Down Expand Up @@ -559,6 +600,9 @@ def pivot_wider(self,
if no_id: out = out.drop('_id')

return out

def print(self):
self.pipe(print)

def pull(self, var = None):
"""
Expand Down Expand Up @@ -858,45 +902,6 @@ def tail(self, n = 5, *, _by = None):
"""Alias for `.slice_tail()`"""
return self.slice_tail(n, _by = _by)

def as_dict(self, *, as_series = True):
"""
Aggregate data with summary statistics
Parameters
----------
as_series : bool
If True - returns the dict values as Series
If False - returns the dict values as lists
Examples
--------
>>> df.to_dict()
>>> df.to_dict(as_series = False)
"""
return super().to_dict(as_series = as_series)

def as_pandas(self):
"""
Convert to a pandas DataFrame
Examples
--------
>>> df.as_pandas()
"""
return self.as_polars().to_pandas()

def as_polars(self):
"""
Convert to a polars DataFrame
Examples
--------
>>> df.as_polars()
"""
self = copy.copy(self)
self.__class__ = pl.DataFrame
return self

def unite(self, col = "_united", unite_cols = [], sep = "_", remove = True):
"""
Unite multiple columns by pasting strings together
Expand Down

0 comments on commit 7378501

Please sign in to comment.