From b455c90600c6be31309d6f182d4e8f8655ee9856 Mon Sep 17 00:00:00 2001 From: Romain Picard Date: Thu, 7 Dec 2023 19:12:07 +0100 Subject: [PATCH] from_pandas: add index option --- rxsci/operators/pandas.py | 6 +++--- tests/operators/test_pandas.py | 22 ++++++++++++++++++++++ 2 files changed, 25 insertions(+), 3 deletions(-) diff --git a/rxsci/operators/pandas.py b/rxsci/operators/pandas.py index a2020ee..8032b43 100644 --- a/rxsci/operators/pandas.py +++ b/rxsci/operators/pandas.py @@ -10,7 +10,7 @@ except Exception: pass - def from_pandas(dataframe, scheduler=None, progress=False): + def from_pandas(dataframe, scheduler=None, progress=False, index=False): """Creates an observable from a pandas dataframe When a dict is provided as the progress argument, it accepts these keys: @@ -31,11 +31,11 @@ def from_pandas(dataframe, scheduler=None, progress=False): tqdm_kwargs['total']=len(dataframe) return rx.from_( - tqdm(dataframe.itertuples(index=False), **tqdm_kwargs), + tqdm(dataframe.itertuples(index=index), **tqdm_kwargs), scheduler=scheduler, ) return rx.from_( - dataframe.itertuples(index=False), + dataframe.itertuples(index=index), scheduler=scheduler, ) diff --git a/tests/operators/test_pandas.py b/tests/operators/test_pandas.py index 2467278..96508ac 100644 --- a/tests/operators/test_pandas.py +++ b/tests/operators/test_pandas.py @@ -33,6 +33,28 @@ def test_from_pandas_base(progress): assert actual_result == 3 +@pytest.mark.parametrize( + "progress", + [ + False, True, {'interval': 60} + ] +) +def test_from_pandas_with_index(progress): + data = [ + Item("foo", 1), + Item("bar", 2), + Item("fiz", 4), + Item("fuz", 4), + ] + + df = pd.DataFrame(data) + actual_result = rs.ops.from_pandas(df, progress=progress, index=True).pipe( + ops.map(lambda i: i.Index), + ops.sum(), + ).run() + + assert actual_result == 6 + def test_to_pandas_base(): data = [ Item("foo", 1),