Skip to content

PERF: TimedeltaArray.__iter__ #36551

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Oct 14, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 13 additions & 3 deletions asv_bench/benchmarks/timeseries.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,14 @@
import dateutil
import numpy as np

from pandas import DataFrame, Series, date_range, period_range, to_datetime
from pandas import (
DataFrame,
Series,
date_range,
period_range,
timedelta_range,
to_datetime,
)

from pandas.tseries.frequencies import infer_freq

Expand Down Expand Up @@ -121,12 +128,15 @@ def time_convert(self):

class Iteration:

params = [date_range, period_range]
params = [date_range, period_range, timedelta_range]
param_names = ["time_index"]

def setup(self, time_index):
N = 10 ** 6
self.idx = time_index(start="20140101", freq="T", periods=N)
if time_index is timedelta_range:
self.idx = time_index(start=0, freq="T", periods=N)
else:
self.idx = time_index(start="20140101", freq="T", periods=N)
self.exit = 10000

def time_iter(self, time_index):
Expand Down
5 changes: 4 additions & 1 deletion pandas/core/arrays/datetimelike.py
Original file line number Diff line number Diff line change
Expand Up @@ -469,7 +469,10 @@ def _box_values(self, values):
return lib.map_infer(values, self._box_func)

def __iter__(self):
return (self._box_func(v) for v in self.asi8)
if self.ndim > 1:
return (self[n] for n in range(len(self)))
else:
return (self._box_func(v) for v in self.asi8)

@property
def asi8(self) -> np.ndarray:
Expand Down
28 changes: 15 additions & 13 deletions pandas/core/arrays/datetimes.py
Original file line number Diff line number Diff line change
Expand Up @@ -558,19 +558,21 @@ def __iter__(self):
------
tstamp : Timestamp
"""

# convert in chunks of 10k for efficiency
data = self.asi8
length = len(self)
chunksize = 10000
chunks = int(length / chunksize) + 1
for i in range(chunks):
start_i = i * chunksize
end_i = min((i + 1) * chunksize, length)
converted = ints_to_pydatetime(
data[start_i:end_i], tz=self.tz, freq=self.freq, box="timestamp"
)
yield from converted
if self.ndim > 1:
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

umm, when is this hit?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

it isn't ATM, but there do exist 2D DTAs so we should handle this correctly

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ok, either leave it out or pls add a test (former is prob better); can followon later i think.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this has a test test_iter_2d

return (self[n] for n in range(len(self)))
else:
# convert in chunks of 10k for efficiency
data = self.asi8
length = len(self)
chunksize = 10000
chunks = int(length / chunksize) + 1
for i in range(chunks):
start_i = i * chunksize
end_i = min((i + 1) * chunksize, length)
converted = ints_to_pydatetime(
data[start_i:end_i], tz=self.tz, freq=self.freq, box="timestamp"
)
yield from converted

def astype(self, dtype, copy=True):
# We handle
Expand Down
21 changes: 20 additions & 1 deletion pandas/core/arrays/timedeltas.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,11 @@
)
from pandas._libs.tslibs.conversion import precision_from_unit
from pandas._libs.tslibs.fields import get_timedelta_field
from pandas._libs.tslibs.timedeltas import array_to_timedelta64, parse_timedelta_unit
from pandas._libs.tslibs.timedeltas import (
array_to_timedelta64,
ints_to_pytimedelta,
parse_timedelta_unit,
)
from pandas.compat.numpy import function as nv

from pandas.core.dtypes.common import (
Expand Down Expand Up @@ -328,6 +332,21 @@ def astype(self, dtype, copy=True):
return self
return dtl.DatetimeLikeArrayMixin.astype(self, dtype, copy=copy)

def __iter__(self):
if self.ndim > 1:
return (self[n] for n in range(len(self)))
else:
# convert in chunks of 10k for efficiency
data = self.asi8
length = len(self)
chunksize = 10000
chunks = int(length / chunksize) + 1
for i in range(chunks):
start_i = i * chunksize
end_i = min((i + 1) * chunksize, length)
converted = ints_to_pytimedelta(data[start_i:end_i], box=True)
yield from converted

# ----------------------------------------------------------------
# Reductions

Expand Down
9 changes: 9 additions & 0 deletions pandas/tests/arrays/test_datetimelike.py
Original file line number Diff line number Diff line change
Expand Up @@ -310,6 +310,15 @@ def test_getitem_2d(self, arr1d):
expected = arr1d[-1]
assert result == expected

def test_iter_2d(self, arr1d):
data2d = arr1d._data[:3, np.newaxis]
arr2d = type(arr1d)._simple_new(data2d, dtype=arr1d.dtype)
result = list(arr2d)
for x in result:
assert isinstance(x, type(arr1d))
assert x.ndim == 1
assert x.dtype == arr1d.dtype

def test_setitem(self):
data = np.arange(10, dtype="i8") * 24 * 3600 * 10 ** 9
arr = self.array_cls(data, freq="D")
Expand Down