markfairbanks / tidypolars

Tidy interface to polars
http://tidypolars.readthedocs.io
MIT License
332 stars 11 forks source link

Update `tp.Tibble()` generation #55

Closed markfairbanks closed 2 years ago

markfairbanks commented 2 years ago
import polars as pl
from polars import col

def _args_as_list(x):
  if len(x) == 0:
      return []
  elif isinstance(x[0], list):
      return x[0]
  elif isinstance(x[0], pl.Series):
      return list(x[0])
  else:
      return [*x]

def _as_list(x):
  if isinstance(x, list):
      return x.copy()
  else:
      return [x]

def tibble(*args, **kwargs):
  args = _args_as_list(args)
  if len(args) == 0:
    kwargs = {key:_as_list(value) for key, value in kwargs.items()}
    return pl.DataFrame(kwargs)
  elif len(args) == 1:
    return pl.DataFrame(args[0])
  else:
    raise ValueError

tibble(x = [1, 2, 3], y = ['a', 'a', 'b'])

Would allow R-like syntax while also allowing generation through a dictionary