Skip to content

Period factorization #14419

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 5 commits into from
Oct 15, 2016
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
26 changes: 26 additions & 0 deletions asv_bench/benchmarks/groupby.py
Original file line number Diff line number Diff line change
Expand Up @@ -548,6 +548,32 @@ def time_groupby_sum(self):
self.df.groupby(['a'])['b'].sum()


class groupby_period(object):
# GH 14338
goal_time = 0.2

def make_grouper(self, N):
return pd.period_range('1900-01-01', freq='D', periods=N)

def setup(self):
N = 10000
self.grouper = self.make_grouper(N)
self.df = pd.DataFrame(np.random.randn(N, 2))

def time_groupby_sum(self):
self.df.groupby(self.grouper).sum()


class groupby_datetime(groupby_period):
def make_grouper(self, N):
return pd.date_range('1900-01-01', freq='D', periods=N)


class groupby_datetimetz(groupby_period):
def make_grouper(self, N):
return pd.date_range('1900-01-01', freq='D', periods=N,
tz='US/Central')

#----------------------------------------------------------------------
# Series.value_counts

Expand Down
2 changes: 1 addition & 1 deletion doc/source/whatsnew/v0.19.1.txt
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ Highlights include:
Performance Improvements
~~~~~~~~~~~~~~~~~~~~~~~~


- Fixed performance regression in factorization of ``Period`` data (:issue:`14338`)



Expand Down
41 changes: 23 additions & 18 deletions pandas/core/algorithms.py
Original file line number Diff line number Diff line change
Expand Up @@ -285,18 +285,27 @@ def factorize(values, sort=False, order=None, na_sentinel=-1, size_hint=None):
note: an array of Periods will ignore sort as it returns an always sorted
PeriodIndex
"""
from pandas import Index, Series, DatetimeIndex

vals = np.asarray(values)

# localize to UTC
is_datetimetz_type = is_datetimetz(values)
if is_datetimetz_type:
values = DatetimeIndex(values)
vals = values.asi8
from pandas import Index, Series, DatetimeIndex, PeriodIndex

# handling two possibilities here
# - for a numpy datetimelike simply view as i8 then cast back
# - for an extension datetimelike view as i8 then
# reconstruct from boxed values to transfer metadata
dtype = None
if needs_i8_conversion(values):
if is_period_dtype(values):
values = PeriodIndex(values)
vals = values.asi8
elif is_datetimetz(values):
values = DatetimeIndex(values)
vals = values.asi8
else:
# numpy dtype
dtype = values.dtype
vals = values.view(np.int64)
else:
vals = np.asarray(values)

is_datetime = is_datetime64_dtype(vals)
is_timedelta = is_timedelta64_dtype(vals)
(hash_klass, vec_klass), vals = _get_data_algo(vals, _hashtables)

table = hash_klass(size_hint or len(vals))
Expand All @@ -311,13 +320,9 @@ def factorize(values, sort=False, order=None, na_sentinel=-1, size_hint=None):
uniques, labels = safe_sort(uniques, labels, na_sentinel=na_sentinel,
assume_unique=True)

if is_datetimetz_type:
# reset tz
uniques = values._shallow_copy(uniques)
elif is_datetime:
uniques = uniques.astype('M8[ns]')
elif is_timedelta:
uniques = uniques.astype('m8[ns]')
if dtype is not None:
uniques = uniques.astype(dtype)

if isinstance(values, Index):
uniques = values._shallow_copy(uniques, name=None)
elif isinstance(values, Series):
Expand Down