-
-
Notifications
You must be signed in to change notification settings - Fork 18.5k
implement astype portion of #24024 #24405
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
Changes from all commits
6a5c216
1a9f30b
1b109b8
f271005
5615b9f
d5cca5a
184f59f
df39bd7
e41068a
6f108dd
b123d08
207ffb9
04efd45
3fca810
5fa32e9
5d718e6
33b5434
e29d898
a3c42f0
eac662b
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -15,8 +15,9 @@ | |
|
||
from pandas.core.dtypes.common import ( | ||
_INT64_DTYPE, _NS_DTYPE, is_categorical_dtype, is_datetime64_dtype, | ||
is_datetime64tz_dtype, is_extension_type, is_float_dtype, is_int64_dtype, | ||
is_object_dtype, is_period_dtype, is_string_dtype, is_timedelta64_dtype) | ||
is_datetime64_ns_dtype, is_datetime64tz_dtype, is_dtype_equal, | ||
is_extension_type, is_float_dtype, is_int64_dtype, is_object_dtype, | ||
is_period_dtype, is_string_dtype, is_timedelta64_dtype, pandas_dtype) | ||
from pandas.core.dtypes.dtypes import DatetimeTZDtype | ||
from pandas.core.dtypes.generic import ABCIndexClass, ABCSeries | ||
from pandas.core.dtypes.missing import isna | ||
|
@@ -473,6 +474,35 @@ def __iter__(self): | |
for v in converted: | ||
yield v | ||
|
||
def astype(self, dtype, copy=True): | ||
# We handle | ||
# --> datetime | ||
# --> period | ||
# DatetimeLikeArrayMixin Super handles the rest. | ||
dtype = pandas_dtype(dtype) | ||
|
||
if (is_datetime64_ns_dtype(dtype) and | ||
not is_dtype_equal(dtype, self.dtype)): | ||
# GH#18951: datetime64_ns dtype but not equal means different tz | ||
new_tz = getattr(dtype, 'tz', None) | ||
if getattr(self.dtype, 'tz', None) is None: | ||
return self.tz_localize(new_tz) | ||
result = self.tz_convert(new_tz) | ||
if new_tz is None: | ||
# Do we want .astype('datetime64[ns]') to be an ndarray. | ||
# The astype in Block._astype expects this to return an | ||
# ndarray, but we could maybe work around it there. | ||
result = result._data | ||
jreback marked this conversation as resolved.
Show resolved
Hide resolved
|
||
return result | ||
elif is_datetime64tz_dtype(self.dtype) and is_dtype_equal(self.dtype, | ||
dtype): | ||
if copy: | ||
return self.copy() | ||
return self | ||
elif is_period_dtype(dtype): | ||
return self.to_period(freq=dtype.freq) | ||
return dtl.DatetimeLikeArrayMixin.astype(self, dtype, copy) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Just noticed... it'd be nice to leave a bunch of Actually... I think Python2 will force us to make this changes when we switch inheritance to composition, since we won't be able to call the unbound method with a DatetimeIndex anymore (I think). |
||
|
||
# ---------------------------------------------------------------- | ||
# ExtensionArray Interface | ||
|
||
|
@@ -495,7 +525,7 @@ def _validate_fill_value(self, fill_value): | |
# ----------------------------------------------------------------- | ||
# Rendering Methods | ||
|
||
def _format_native_types(self, na_rep=u'NaT', date_format=None, **kwargs): | ||
def _format_native_types(self, na_rep='NaT', date_format=None, **kwargs): | ||
from pandas.io.formats.format import _get_format_datetime64_from_values | ||
fmt = _get_format_datetime64_from_values(self, date_format) | ||
|
||
|
Uh oh!
There was an error while loading. Please reload this page.