Skip to content

BUG: DatetimeIndex.__iter__ creates a temp array of Timestamp (GH7683) #7709

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

Closed
wants to merge 1 commit into from
Closed
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
1 change: 1 addition & 0 deletions doc/source/v0.15.0.txt
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,7 @@ There are no experimental changes in 0.15.0
Bug Fixes
~~~~~~~~~

- Bug in ``DatetimeIndex``, ``__iter__`` creates a temp array of ``Timestamp`` (:issue:`7683`)



Expand Down
3 changes: 3 additions & 0 deletions pandas/core/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -387,6 +387,9 @@ def _ops_compat(self, name, op_accessor):
is_year_start = _field_accessor('is_year_start', "Logical indicating if first day of year (defined by frequency)")
is_year_end = _field_accessor('is_year_end', "Logical indicating if last day of year (defined by frequency)")

def __iter__(self):
return (self._box_func(v) for v in self.asi8)

@property
def _box_func(self):
"""
Expand Down
3 changes: 0 additions & 3 deletions pandas/tseries/index.py
Original file line number Diff line number Diff line change
Expand Up @@ -1476,9 +1476,6 @@ def normalize(self):
return DatetimeIndex(new_values, freq='infer', name=self.name,
tz=self.tz)

def __iter__(self):
return iter(self.asobject)

def searchsorted(self, key, side='left'):
if isinstance(key, np.ndarray):
key = np.array(key, dtype=_NS_DTYPE, copy=False)
Expand Down
4 changes: 0 additions & 4 deletions pandas/tseries/period.py
Original file line number Diff line number Diff line change
Expand Up @@ -738,10 +738,6 @@ def astype(self, dtype):
return Index(self.values, dtype)
raise ValueError('Cannot cast PeriodIndex to dtype %s' % dtype)

def __iter__(self):
for val in self.values:
yield Period(ordinal=val, freq=self.freq)

def searchsorted(self, key, side='left'):
if isinstance(key, compat.string_types):
key = Period(key, freq=self.freq).ordinal
Expand Down
25 changes: 25 additions & 0 deletions vb_suite/timeseries.py
Original file line number Diff line number Diff line change
Expand Up @@ -333,3 +333,28 @@ def date_range(start=None, end=None, periods=None, freq=None):

timeseries_is_month_start = Benchmark('rng.is_month_start', setup,
start_date=datetime(2014, 4, 1))

#----------------------------------------------------------------------
# iterate over DatetimeIndex/PeriodIndex
setup = common_setup + """
N = 1000000
Copy link
Contributor

Choose a reason for hiding this comment

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

using the smaller is fine

M = 10000
idx1 = date_range(start='20140101', freq='T', periods=N)
idx2 = period_range(start='20140101', freq='T', periods=N)

def iter_n(iterable, n=None):
Copy link
Contributor

Choose a reason for hiding this comment

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

you don't need this, the vbench will run on different versions

i = 0
for _ in iterable:
i += 1
if n is not None and i > n:
break
"""

timeseries_iter_datetimeindex = Benchmark('iter_n(idx1)', setup)
Copy link
Contributor

Choose a reason for hiding this comment

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

just use iter(idx1)


timeseries_iter_periodindex = Benchmark('iter_n(idx2)', setup)

timeseries_iter_datetimeindex_preexit = Benchmark('iter_n(idx1, M)', setup)
Copy link
Contributor

Choose a reason for hiding this comment

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

you don't need these last 2 (the first 2 will suffice)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

the change fixes setup time and memory for iterator but it uses a bit more time when iterating over the whole list. the first two tests cover full iteration while the last two cover partial iteration.

Copy link
Contributor

Choose a reason for hiding this comment

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

I c what you are going for, ok then! (though I would still use iter(idx1) on the first 2

Copy link
Contributor Author

Choose a reason for hiding this comment

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

iter(idx1) is basically partial iteration with non element iterated.


timeseries_iter_periodindex_preexit = Benchmark('iter_n(idx2, M)', setup)