Skip to content

Commit

Permalink
from_pandas: add index option
Browse files Browse the repository at this point in the history
  • Loading branch information
MainRo committed Dec 7, 2023
1 parent ee2452a commit b455c90
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 3 deletions.
6 changes: 3 additions & 3 deletions rxsci/operators/pandas.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand All @@ -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,
)

Expand Down
22 changes: 22 additions & 0 deletions tests/operators/test_pandas.py
Original file line number Diff line number Diff line change
Expand Up @@ -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),
Expand Down

0 comments on commit b455c90

Please sign in to comment.