From a08265da097e92d09b97bed5ff14f8ac923b9cac Mon Sep 17 00:00:00 2001 From: markfairbanks Date: Mon, 23 Sep 2024 02:15:02 -0600 Subject: [PATCH 1/6] Update changelog --- CHANGELOG.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 3063dbc..d175892 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -7,9 +7,11 @@ #### Functionality improvements * Convert `by` arg `_by` to allow naming columns `by` in `.mutate()`/`.summarize()` +* Convert `.to_pandas()`/`.to_polars()` to `.as_pandas()`/`.as_polars()` #### New functions +* `as_tibble()` * `where()` ## v0.2.19 From 249c810c28b2740a1db7f6c93fa30fb642201b1b Mon Sep 17 00:00:00 2001 From: markfairbanks Date: Mon, 23 Sep 2024 02:22:46 -0600 Subject: [PATCH 2/6] Create `as_tibble()` --- tidypolars/tibble_df.py | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/tidypolars/tibble_df.py b/tidypolars/tibble_df.py index ba8c19b..8ff9224 100644 --- a/tidypolars/tibble_df.py +++ b/tidypolars/tibble_df.py @@ -15,6 +15,7 @@ from operator import not_ __all__ = [ + "as_tibble", "tibble", "desc", "from_pandas", "from_polars" @@ -959,6 +960,15 @@ def desc(x): class DescCol(pl.Expr): pass +def as_tibble(x): + if isinstance(x, pl.DataFrame): + out = from_polars(x) + elif isinstance(x, dict): + out = tibble(x) + else: + out = pl.from_dataframe(x) + return out + def from_polars(df): """ Convert from polars DataFrame to tibble From 32d8c93822404a6e013f6449b4e6d3ac4a206ea5 Mon Sep 17 00:00:00 2001 From: markfairbanks Date: Mon, 23 Sep 2024 02:31:56 -0600 Subject: [PATCH 3/6] Add `is_tibble()` --- tidypolars/tibble_df.py | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) diff --git a/tidypolars/tibble_df.py b/tidypolars/tibble_df.py index 8ff9224..ae3f3ac 100644 --- a/tidypolars/tibble_df.py +++ b/tidypolars/tibble_df.py @@ -16,6 +16,7 @@ __all__ = [ "as_tibble", + "is_tibble", "tibble", "desc", "from_pandas", "from_polars" @@ -961,6 +962,18 @@ class DescCol(pl.Expr): pass def as_tibble(x): + """ + Convert an object to a tibble + + Parameters + ---------- + x : [pl.DataFrame, pd.DataFrame, dict] + Object to convert to a tibble + + Examples + -------- + >>> tp.as_tibble(polars_df) + """ if isinstance(x, pl.DataFrame): out = from_polars(x) elif isinstance(x, dict): @@ -969,6 +982,20 @@ def as_tibble(x): out = pl.from_dataframe(x) return out +def is_tibble(x): + """ + Is an object to a tibble + + Parameters + ---------- + x : object + + Examples + -------- + >>> tp.is_tibble(df) + """ + return isinstance(x, tibble) + def from_polars(df): """ Convert from polars DataFrame to tibble From 873545337eeb1eeac1b59813c79440aaac43612c Mon Sep 17 00:00:00 2001 From: markfairbanks Date: Mon, 23 Sep 2024 02:32:05 -0600 Subject: [PATCH 4/6] Add tests --- tests/test_tibble.py | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/tests/test_tibble.py b/tests/test_tibble.py index db2c7b3..e5f04f3 100644 --- a/tests/test_tibble.py +++ b/tests/test_tibble.py @@ -463,3 +463,12 @@ def test_funs_in_a_row(): df.tail() df.arrange('x', 'y') assert True, "Functions in a row failed" + +def test_conversion(): + """Can convert""" + df = tp.tibble(a = ["a", "a", "a"], b = ["b", "b", "b"], c = range(3)) + actual = df.as_polars() + expected = pl.DataFrame(dict(a = ["a", "a", "a"], b = ["b", "b", "b"], c = range(3))) + assert actual.equals(expected), "as_polars failed" + assert tp.is_tibble(df) == True, "is_tibble failed" + assert tp.as_tibble(actual).equals(df), "as_tibble failed" From 1e38d0d9f5544c92e7a55638588fcbd3e50a311c Mon Sep 17 00:00:00 2001 From: markfairbanks Date: Mon, 23 Sep 2024 02:32:11 -0600 Subject: [PATCH 5/6] Update changelog --- CHANGELOG.md | 1 + 1 file changed, 1 insertion(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index d175892..c094d87 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -12,6 +12,7 @@ #### New functions * `as_tibble()` +* `is_tibble()` * `where()` ## v0.2.19 From 946dc190d7321394875db14bb5fe3f012fe73f48 Mon Sep 17 00:00:00 2001 From: markfairbanks Date: Mon, 23 Sep 2024 02:34:22 -0600 Subject: [PATCH 6/6] `as_tibble()` leaves tibbles alone --- tidypolars/tibble_df.py | 2 ++ 1 file changed, 2 insertions(+) diff --git a/tidypolars/tibble_df.py b/tidypolars/tibble_df.py index ae3f3ac..bdd2391 100644 --- a/tidypolars/tibble_df.py +++ b/tidypolars/tibble_df.py @@ -978,6 +978,8 @@ def as_tibble(x): out = from_polars(x) elif isinstance(x, dict): out = tibble(x) + elif is_tibble(x): + out = x else: out = pl.from_dataframe(x) return out