Skip to content

Commit 4516e97

Browse files
jbrockmendelPingviinituutti
authored andcommitted
ensure DatetimeTZBlock always gets a DatetimeArray (pandas-dev#24622)
1 parent 3b17ba5 commit 4516e97

File tree

2 files changed

+14
-24
lines changed

2 files changed

+14
-24
lines changed

pandas/core/internals/blocks.py

+5-22
Original file line numberDiff line numberDiff line change
@@ -2242,44 +2242,26 @@ class DatetimeTZBlock(ExtensionBlock, DatetimeBlock):
22422242
is_datetimetz = True
22432243
is_extension = True
22442244

2245-
def __init__(self, values, placement, ndim=2, dtype=None):
2246-
# XXX: This will end up calling _maybe_coerce_values twice
2247-
# when dtype is not None. It's relatively cheap (just an isinstance)
2248-
# but it'd nice to avoid.
2249-
#
2250-
# If we can remove dtype from __init__, and push that conversion
2251-
# push onto the callers, then we can remove this entire __init__
2252-
# and just use DatetimeBlock's.
2253-
if dtype is not None:
2254-
values = self._maybe_coerce_values(values, dtype=dtype)
2255-
super(DatetimeTZBlock, self).__init__(values, placement=placement,
2256-
ndim=ndim)
2257-
22582245
@property
22592246
def _holder(self):
22602247
return DatetimeArray
22612248

2262-
def _maybe_coerce_values(self, values, dtype=None):
2249+
def _maybe_coerce_values(self, values):
22632250
"""Input validation for values passed to __init__. Ensure that
22642251
we have datetime64TZ, coercing if necessary.
22652252
22662253
Parametetrs
22672254
-----------
22682255
values : array-like
22692256
Must be convertible to datetime64
2270-
dtype : string or DatetimeTZDtype, optional
2271-
Does a shallow copy to this tz
22722257
22732258
Returns
22742259
-------
2275-
values : ndarray[datetime64ns]
2260+
values : DatetimeArray
22762261
"""
22772262
if not isinstance(values, self._holder):
22782263
values = self._holder(values)
22792264

2280-
if dtype is not None:
2281-
values = type(values)(values, dtype=dtype)
2282-
22832265
if values.tz is None:
22842266
raise ValueError("cannot create a DatetimeTZBlock without a tz")
22852267

@@ -3094,8 +3076,9 @@ def make_block(values, placement, klass=None, ndim=None, dtype=None,
30943076
klass = get_block_type(values, dtype)
30953077

30963078
elif klass is DatetimeTZBlock and not is_datetime64tz_dtype(values):
3097-
return klass(values, ndim=ndim,
3098-
placement=placement, dtype=dtype)
3079+
# TODO: This is no longer hit internally; does it need to be retained
3080+
# for e.g. pyarrow?
3081+
values = DatetimeArray(values, dtype)
30993082

31003083
return klass(values, ndim=ndim, placement=placement)
31013084

pandas/io/packers.py

+9-2
Original file line numberDiff line numberDiff line change
@@ -53,14 +53,15 @@
5353
BadMove as _BadMove, move_into_mutable_buffer as _move_into_mutable_buffer)
5454

5555
from pandas.core.dtypes.common import (
56-
is_categorical_dtype, is_object_dtype, needs_i8_conversion, pandas_dtype)
56+
is_categorical_dtype, is_datetime64tz_dtype, is_object_dtype,
57+
needs_i8_conversion, pandas_dtype)
5758

5859
from pandas import ( # noqa:F401
5960
Categorical, CategoricalIndex, DataFrame, DatetimeIndex, Float64Index,
6061
Index, Int64Index, Interval, IntervalIndex, MultiIndex, NaT, Panel, Period,
6162
PeriodIndex, RangeIndex, Series, TimedeltaIndex, Timestamp)
6263
from pandas.core import internals
63-
from pandas.core.arrays import IntervalArray, PeriodArray
64+
from pandas.core.arrays import DatetimeArray, IntervalArray, PeriodArray
6465
from pandas.core.arrays.sparse import BlockIndex, IntIndex
6566
from pandas.core.generic import NDFrame
6667
from pandas.core.internals import BlockManager, _safe_reshape, make_block
@@ -651,6 +652,12 @@ def create_block(b):
651652
placement = b[u'locs']
652653
else:
653654
placement = axes[0].get_indexer(b[u'items'])
655+
656+
if is_datetime64tz_dtype(b[u'dtype']):
657+
assert isinstance(values, np.ndarray), type(values)
658+
assert values.dtype == 'M8[ns]', values.dtype
659+
values = DatetimeArray(values, dtype=b[u'dtype'])
660+
654661
return make_block(values=values,
655662
klass=getattr(internals, b[u'klass']),
656663
placement=placement,

0 commit comments

Comments
 (0)