Skip to content

REF: get reshape kludge out of dtypes.cast #40203

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 4 commits into from
Mar 4, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
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
14 changes: 4 additions & 10 deletions pandas/core/dtypes/cast.py
Original file line number Diff line number Diff line change
Expand Up @@ -1529,16 +1529,10 @@ def try_datetime(v: np.ndarray) -> ArrayLike:
# e.g. <class 'numpy.timedelta64'> is not convertible to datetime
return v.reshape(shape)
else:
if dta.dtype == object or dta.tz is None:
# GH#19671 if we have mixed timezones we may have object-dtype
# here.
# This is reachable bc allow_object=True, means we cast things
# to mixed-tz datetime objects (mostly). Only 1 test
# relies on this behavior, see GH#40111
# FIXME: conditional reshape is kludgy
return np.asarray(dta).reshape(shape)
# otherwise we have dt64tz
return dta
# GH#19761 we may have mixed timezones, in which cast 'dta' is
# an ndarray[object]. Only 1 test
# relies on this behavior, see GH#40111
return dta.reshape(shape)

def try_timedelta(v: np.ndarray) -> np.ndarray:
# safe coerce to timedelta64
Expand Down
30 changes: 23 additions & 7 deletions pandas/core/internals/construction.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,10 @@
algorithms,
common as com,
)
from pandas.core.arrays import Categorical
from pandas.core.arrays import (
Categorical,
DatetimeArray,
)
from pandas.core.construction import (
extract_array,
sanitize_array,
Expand All @@ -70,6 +73,10 @@
union_indexes,
)
from pandas.core.internals.array_manager import ArrayManager
from pandas.core.internals.blocks import (
ensure_block_shape,
make_block,
)
from pandas.core.internals.managers import (
BlockManager,
create_block_manager_from_arrays,
Expand Down Expand Up @@ -288,27 +295,36 @@ def ndarray_to_mgr(
# transpose and separate blocks

dvals_list = [maybe_infer_to_datetimelike(row) for row in values]
for n in range(len(dvals_list)):
if isinstance(dvals_list[n], np.ndarray):
dvals_list[n] = dvals_list[n].reshape(1, -1)

from pandas.core.internals.blocks import make_block
dvals_list = [ensure_block_shape(dval, 2) for dval in dvals_list]

# TODO: What about re-joining object columns?
dvals_list = [maybe_squeeze_dt64tz(x) for x in dvals_list]
block_values = [
make_block(dvals_list[n], placement=[n], ndim=2)
for n in range(len(dvals_list))
]

else:
datelike_vals = maybe_infer_to_datetimelike(values)
datelike_vals = maybe_squeeze_dt64tz(datelike_vals)
block_values = [datelike_vals]
else:
block_values = [values]
block_values = [maybe_squeeze_dt64tz(values)]

return create_block_manager_from_blocks(block_values, [columns, index])


def maybe_squeeze_dt64tz(dta: ArrayLike) -> ArrayLike:
"""
If we have a tzaware DatetimeArray with shape (1, N), squeeze to (N,)
"""
# TODO(EA2D): kludge not needed with 2D EAs
if isinstance(dta, DatetimeArray) and dta.ndim == 2 and dta.tz is not None:
assert dta.shape[0] == 1
dta = dta[0]
return dta


def dict_to_mgr(
data: Dict, index, columns, dtype: Optional[DtypeObj], typ: str
) -> Manager:
Expand Down