Skip to content

Commit 3cdb115

Browse files
committed
ENH: Add PeriodBlock
1 parent f26b049 commit 3cdb115

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

43 files changed

+1099
-316
lines changed

pandas/core/algorithms.py

+15-26
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,8 @@
1414
is_categorical_dtype,
1515
is_extension_type,
1616
is_datetimetz,
17+
is_period,
1718
is_period_dtype,
18-
is_period_arraylike,
1919
is_float_dtype,
2020
needs_i8_conversion,
2121
is_categorical,
@@ -367,7 +367,8 @@ def value_counts(values, sort=True, ascending=False, normalize=False,
367367
raise TypeError("bins argument only works with numeric data.")
368368
values = cat.codes
369369

370-
if is_extension_type(values) and not is_datetimetz(values):
370+
if (is_extension_type(values) and
371+
not (is_datetimetz(values) or is_period(values))):
371372
# handle Categorical and sparse,
372373
# datetime tz can be handeled in ndarray path
373374
result = Series(values).values.value_counts(dropna=dropna)
@@ -399,25 +400,14 @@ def value_counts(values, sort=True, ascending=False, normalize=False,
399400

400401
def _value_counts_arraylike(values, dropna=True):
401402
is_datetimetz_type = is_datetimetz(values)
402-
is_period_type = (is_period_dtype(values) or
403-
is_period_arraylike(values))
404-
403+
is_period_type = is_period_dtype(values)
405404
orig = values
406405

407406
from pandas.core.series import Series
408-
values = Series(values).values
407+
values = Series(values)._values
409408
dtype = values.dtype
410409

411-
if needs_i8_conversion(dtype) or is_period_type:
412-
413-
from pandas.tseries.index import DatetimeIndex
414-
from pandas.tseries.period import PeriodIndex
415-
416-
if is_period_type:
417-
# values may be an object
418-
values = PeriodIndex(values)
419-
freq = values.freq
420-
410+
if needs_i8_conversion(dtype):
421411
values = values.view(np.int64)
422412
keys, counts = htable.value_count_int64(values, dropna)
423413

@@ -426,13 +416,14 @@ def _value_counts_arraylike(values, dropna=True):
426416
keys, counts = keys[msk], counts[msk]
427417

428418
# convert the keys back to the dtype we came in
429-
keys = keys.astype(dtype)
430-
431-
# dtype handling
432419
if is_datetimetz_type:
420+
from pandas.tseries.index import DatetimeIndex
433421
keys = DatetimeIndex._simple_new(keys, tz=orig.dtype.tz)
434-
if is_period_type:
435-
keys = PeriodIndex._simple_new(keys, freq=freq)
422+
elif is_period_type:
423+
from pandas.tseries.period import PeriodIndex
424+
keys = PeriodIndex._simple_new(keys, freq=orig.dtype.freq)
425+
else:
426+
keys = keys.astype(dtype)
436427

437428
elif is_integer_dtype(dtype):
438429
values = _ensure_int64(values)
@@ -476,9 +467,6 @@ def duplicated(values, keep='first'):
476467
# no need to revert to original type
477468
if needs_i8_conversion(dtype):
478469
values = values.view(np.int64)
479-
elif is_period_arraylike(values):
480-
from pandas.tseries.period import PeriodIndex
481-
values = PeriodIndex(values).asi8
482470
elif is_categorical_dtype(dtype):
483471
values = values.values.codes
484472
elif isinstance(values, (ABCSeries, ABCIndex)):
@@ -1015,8 +1003,9 @@ def take_nd(arr, indexer, axis=0, out=None, fill_value=np.nan, mask_info=None,
10151003
if is_categorical(arr):
10161004
return arr.take_nd(indexer, fill_value=fill_value,
10171005
allow_fill=allow_fill)
1018-
elif is_datetimetz(arr):
1019-
return arr.take(indexer, fill_value=fill_value, allow_fill=allow_fill)
1006+
elif is_extension_type(arr):
1007+
return arr.take(indexer, fill_value=fill_value,
1008+
allow_fill=allow_fill)
10201009

10211010
if indexer is None:
10221011
indexer = np.arange(arr.shape[axis], dtype=np.int64)

pandas/core/frame.py

+10-1
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@
3939
is_datetimetz,
4040
is_datetime64_dtype,
4141
is_datetime64tz_dtype,
42+
is_period_dtype,
4243
is_bool_dtype,
4344
is_integer_dtype,
4445
is_float_dtype,
@@ -262,8 +263,10 @@ def __init__(self, data=None, index=None, columns=None, dtype=None,
262263
if isinstance(data, BlockManager):
263264
mgr = self._init_mgr(data, axes=dict(index=index, columns=columns),
264265
dtype=dtype, copy=copy)
266+
265267
elif isinstance(data, dict):
266268
mgr = self._init_dict(data, index, columns, dtype=dtype)
269+
267270
elif isinstance(data, ma.MaskedArray):
268271
import numpy.ma.mrecords as mrecords
269272
# masked recarray
@@ -2975,7 +2978,7 @@ def reset_index(self, level=None, drop=False, inplace=False, col_level=0,
29752978

29762979
def _maybe_casted_values(index, labels=None):
29772980
if isinstance(index, PeriodIndex):
2978-
values = index.asobject.values
2981+
values = index
29792982
elif isinstance(index, DatetimeIndex) and index.tz is not None:
29802983
values = index
29812984
else:
@@ -3740,6 +3743,11 @@ def combine(self, other, func, fill_value=None, overwrite=True):
37403743
# see if we need to be represented as i8 (datetimelike)
37413744
# try to keep us at this dtype
37423745
needs_i8_conversion_i = needs_i8_conversion(new_dtype)
3746+
3747+
if is_period_dtype(new_dtype):
3748+
# temp for PeriodDtype
3749+
needs_i8_conversion_i = False
3750+
37433751
if needs_i8_conversion_i:
37443752
arr = func(series, otherSeries, True)
37453753
else:
@@ -3790,6 +3798,7 @@ def combine_first(self, other):
37903798
"""
37913799

37923800
def combiner(x, y, needs_i8_conversion=False):
3801+
# ToDo:
37933802
x_values = x.values if hasattr(x, 'values') else x
37943803
y_values = y.values if hasattr(y, 'values') else y
37953804
if needs_i8_conversion:

0 commit comments

Comments
 (0)