Skip to content

Commit e89b0a6

Browse files
committed
ENH: Add PeriodBlock
1 parent b895968 commit e89b0a6

Some content is hidden

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

42 files changed

+1005
-1153
lines changed

pandas/core/algorithms.py

+15-26
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,8 @@
1616
is_categorical_dtype,
1717
is_extension_type,
1818
is_datetimetz,
19+
is_period,
1920
is_period_dtype,
20-
is_period_arraylike,
2121
is_float_dtype,
2222
needs_i8_conversion,
2323
is_categorical,
@@ -410,7 +410,8 @@ def value_counts(values, sort=True, ascending=False, normalize=False,
410410
raise TypeError("bins argument only works with numeric data.")
411411
values = cat.codes
412412

413-
if is_extension_type(values) and not is_datetimetz(values):
413+
if (is_extension_type(values) and
414+
not (is_datetimetz(values) or is_period(values))):
414415
# handle Categorical and sparse,
415416
# datetime tz can be handeled in ndarray path
416417
result = Series(values).values.value_counts(dropna=dropna)
@@ -442,25 +443,14 @@ def value_counts(values, sort=True, ascending=False, normalize=False,
442443

443444
def _value_counts_arraylike(values, dropna=True):
444445
is_datetimetz_type = is_datetimetz(values)
445-
is_period_type = (is_period_dtype(values) or
446-
is_period_arraylike(values))
447-
446+
is_period_type = is_period_dtype(values)
448447
orig = values
449448

450449
from pandas.core.series import Series
451-
values = Series(values).values
450+
values = Series(values)._values
452451
dtype = values.dtype
453452

454-
if needs_i8_conversion(dtype) or is_period_type:
455-
456-
from pandas.tseries.index import DatetimeIndex
457-
from pandas.tseries.period import PeriodIndex
458-
459-
if is_period_type:
460-
# values may be an object
461-
values = PeriodIndex(values)
462-
freq = values.freq
463-
453+
if needs_i8_conversion(dtype):
464454
values = values.view(np.int64)
465455
keys, counts = htable.value_count_int64(values, dropna)
466456

@@ -469,13 +459,14 @@ def _value_counts_arraylike(values, dropna=True):
469459
keys, counts = keys[msk], counts[msk]
470460

471461
# convert the keys back to the dtype we came in
472-
keys = keys.astype(dtype)
473-
474-
# dtype handling
475462
if is_datetimetz_type:
463+
from pandas.tseries.index import DatetimeIndex
476464
keys = DatetimeIndex._simple_new(keys, tz=orig.dtype.tz)
477-
if is_period_type:
478-
keys = PeriodIndex._simple_new(keys, freq=freq)
465+
elif is_period_type:
466+
from pandas.tseries.period import PeriodIndex
467+
keys = PeriodIndex._simple_new(keys, freq=orig.dtype.freq)
468+
else:
469+
keys = keys.astype(dtype)
479470

480471
elif is_integer_dtype(dtype):
481472
values = _ensure_int64(values)
@@ -522,9 +513,6 @@ def duplicated(values, keep='first'):
522513
# no need to revert to original type
523514
if needs_i8_conversion(dtype):
524515
values = values.view(np.int64)
525-
elif is_period_arraylike(values):
526-
from pandas.tseries.period import PeriodIndex
527-
values = PeriodIndex(values).asi8
528516
elif is_categorical_dtype(dtype):
529517
values = values.values.codes
530518
elif isinstance(values, (ABCSeries, ABCIndex)):
@@ -1243,8 +1231,9 @@ def take_nd(arr, indexer, axis=0, out=None, fill_value=np.nan, mask_info=None,
12431231
if is_categorical(arr):
12441232
return arr.take_nd(indexer, fill_value=fill_value,
12451233
allow_fill=allow_fill)
1246-
elif is_datetimetz(arr):
1247-
return arr.take(indexer, fill_value=fill_value, allow_fill=allow_fill)
1234+
elif is_extension_type(arr):
1235+
return arr.take(indexer, fill_value=fill_value,
1236+
allow_fill=allow_fill)
12481237

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

pandas/core/frame.py

+10-1
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@
3838
is_datetimetz,
3939
is_datetime64_dtype,
4040
is_datetime64tz_dtype,
41+
is_period_dtype,
4142
is_bool_dtype,
4243
is_integer_dtype,
4344
is_float_dtype,
@@ -263,8 +264,10 @@ def __init__(self, data=None, index=None, columns=None, dtype=None,
263264
if isinstance(data, BlockManager):
264265
mgr = self._init_mgr(data, axes=dict(index=index, columns=columns),
265266
dtype=dtype, copy=copy)
267+
266268
elif isinstance(data, dict):
267269
mgr = self._init_dict(data, index, columns, dtype=dtype)
270+
268271
elif isinstance(data, ma.MaskedArray):
269272
import numpy.ma.mrecords as mrecords
270273
# masked recarray
@@ -2946,7 +2949,7 @@ def reset_index(self, level=None, drop=False, inplace=False, col_level=0,
29462949

29472950
def _maybe_casted_values(index, labels=None):
29482951
if isinstance(index, PeriodIndex):
2949-
values = index.asobject.values
2952+
values = index
29502953
elif isinstance(index, DatetimeIndex) and index.tz is not None:
29512954
values = index
29522955
else:
@@ -3702,6 +3705,11 @@ def combine(self, other, func, fill_value=None, overwrite=True):
37023705
# see if we need to be represented as i8 (datetimelike)
37033706
# try to keep us at this dtype
37043707
needs_i8_conversion_i = needs_i8_conversion(new_dtype)
3708+
3709+
if is_period_dtype(new_dtype):
3710+
# temp for PeriodDtype
3711+
needs_i8_conversion_i = False
3712+
37053713
if needs_i8_conversion_i:
37063714
arr = func(series, otherSeries, True)
37073715
else:
@@ -3752,6 +3760,7 @@ def combine_first(self, other):
37523760
"""
37533761

37543762
def combiner(x, y, needs_i8_conversion=False):
3763+
# ToDo:
37553764
x_values = x.values if hasattr(x, 'values') else x
37563765
y_values = y.values if hasattr(y, 'values') else y
37573766
if needs_i8_conversion:

0 commit comments

Comments
 (0)