Skip to content

BUG: Ensure .astype doesn't use PandasArray #24866

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
Apr 28, 2019
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
8 changes: 5 additions & 3 deletions pandas/core/internals/blocks.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@

import pandas.core.algorithms as algos
from pandas.core.arrays import (
Categorical, DatetimeArray, ExtensionArray, TimedeltaArray)
Categorical, DatetimeArray, ExtensionArray, PandasDtype, TimedeltaArray)
from pandas.core.base import PandasObject
import pandas.core.common as com
from pandas.core.indexes.datetimes import DatetimeIndex
Expand Down Expand Up @@ -530,6 +530,10 @@ def f(m, v, i):
return self.split_and_operate(None, f, False)

def astype(self, dtype, copy=False, errors='raise', values=None, **kwargs):
dtype = pandas_dtype(dtype)
if isinstance(dtype, PandasDtype):
dtype = dtype.numpy_dtype

return self._astype(dtype, copy=copy, errors=errors, values=values,
**kwargs)

Expand Down Expand Up @@ -591,8 +595,6 @@ def _astype(self, dtype, copy=False, errors='raise', values=None,

return self.make_block(Categorical(self.values, dtype=dtype))

# convert dtypes if needed
dtype = pandas_dtype(dtype)
# astype processing
if is_dtype_equal(self.dtype, dtype):
if copy:
Expand Down
5 changes: 5 additions & 0 deletions pandas/tests/series/test_internals.py
Original file line number Diff line number Diff line change
Expand Up @@ -315,6 +315,11 @@ def test_constructor_no_pandas_array(self):
tm.assert_series_equal(ser, result)
assert isinstance(result._data.blocks[0], IntBlock)

def test_astype_no_pandas_dtype(self):
ser = pd.Series([1, 2], dtype="int64")
result = ser.astype(ser.array.dtype)
tm.assert_series_equal(result, ser)

def test_from_array(self):
result = pd.Series(pd.array(['1H', '2H'], dtype='timedelta64[ns]'))
assert result._data.blocks[0].is_extension is False
Expand Down