Skip to content

Commit

Permalink
fix(python): don't convert "ns"-precision temporal types via pyarrow
Browse files Browse the repository at this point in the history
  • Loading branch information
alexander-beedie committed Jan 31, 2023
1 parent 8863ec5 commit 8cdbdb6
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 7 deletions.
14 changes: 7 additions & 7 deletions py-polars/polars/internals/dataframe/frame.py
Original file line number Diff line number Diff line change
Expand Up @@ -6771,18 +6771,13 @@ def iter_rows(
Returns
-------
An iterator of tuples (default) or dictionaries of row values.
An iterator of tuples (default) or dictionaries of python row values.
Warnings
--------
Row iteration is not optimal as the underlying data is stored in columnar form;
where possible, prefer export via one of the dedicated export/output methods.
Notes
-----
If you are planning to materialise all frame data at once you should prefer
calling ``rows()``, which will be faster.
Examples
--------
>>> df = pl.DataFrame(
Expand All @@ -6809,7 +6804,12 @@ def iter_rows(
if buffer_size:
for offset in range(0, self.height, buffer_size):
zerocopy_slice = self.slice(offset, buffer_size)
if named and _PYARROW_AVAILABLE:
if (
named
and _PYARROW_AVAILABLE
# note: 'ns' precision instantiates values as pandas types - avoid
and not any((getattr(tp, "tu", None) == "ns") for tp in self.dtypes)
):
yield from zerocopy_slice.to_arrow().to_batches()[0].to_pylist()
else:
rows_chunk = zerocopy_slice.rows(named=False)
Expand Down
19 changes: 19 additions & 0 deletions py-polars/tests/unit/test_datelike.py
Original file line number Diff line number Diff line change
Expand Up @@ -356,6 +356,25 @@ def test_timezone() -> None:
assert s.cast(int).series_equal(tz_s.cast(int))


def test_to_dicts() -> None:
now = datetime.now()
data = {
"a": now,
"b": now.date(),
"c": now.time(),
"d": timedelta(days=1, seconds=43200),
}
df = pl.DataFrame(
data, schema_overrides={"a": pl.Datetime("ns"), "d": pl.Duration("ns")}
)
assert len(df) == 1

d = df.to_dicts()[0]
for col in data:
assert d[col] == data[col]
assert isinstance(d[col], type(data[col]))


def test_to_list() -> None:
s = pl.Series("date", [123543, 283478, 1243]).cast(pl.Date)

Expand Down

0 comments on commit 8cdbdb6

Please sign in to comment.