Skip to content

ENH/BUG: DataFrame(object_array_of_periods_or_intervals) #41812

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
Jun 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
1 change: 1 addition & 0 deletions doc/source/whatsnew/v1.3.0.rst
Original file line number Diff line number Diff line change
Expand Up @@ -892,6 +892,7 @@ Conversion
- Bug in :meth:`qcut` raising error when taking ``Float64DType`` as input (:issue:`40730`)
- Bug in :class:`DataFrame` and :class:`Series` construction with ``datetime64[ns]`` data and ``dtype=object`` resulting in ``datetime`` objects instead of :class:`Timestamp` objects (:issue:`41599`)
- Bug in :class:`DataFrame` and :class:`Series` construction with ``timedelta64[ns]`` data and ``dtype=object`` resulting in ``np.timedelta64`` objects instead of :class:`Timedelta` objects (:issue:`41599`)
- Bug in :class:`DataFrame` construction when given a two-dimensional object-dtype ``np.ndarray`` of :class:`Period` or :class:`Interval` objects failing to cast to :class:`PeriodDtype` or :class:`IntervalDtype`, respectively (:issue:`41812`)

Strings
^^^^^^^
Expand Down
5 changes: 5 additions & 0 deletions pandas/_libs/lib.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@ def maybe_convert_objects(
convert_datetime: Literal[False] = ...,
convert_timedelta: bool = ...,
convert_period: Literal[False] = ...,
convert_interval: Literal[False] = ...,
convert_to_nullable_integer: Literal[False] = ...,
dtype_if_all_nat: DtypeObj | None = ...,
) -> np.ndarray: ...
Expand All @@ -86,6 +87,7 @@ def maybe_convert_objects(
convert_datetime: bool = ...,
convert_timedelta: bool = ...,
convert_period: bool = ...,
convert_interval: bool = ...,
convert_to_nullable_integer: Literal[True] = ...,
dtype_if_all_nat: DtypeObj | None = ...,
) -> ArrayLike: ...
Expand All @@ -99,6 +101,7 @@ def maybe_convert_objects(
convert_datetime: Literal[True] = ...,
convert_timedelta: bool = ...,
convert_period: bool = ...,
convert_interval: bool = ...,
convert_to_nullable_integer: bool = ...,
dtype_if_all_nat: DtypeObj | None = ...,
) -> ArrayLike: ...
Expand All @@ -112,6 +115,7 @@ def maybe_convert_objects(
convert_datetime: bool = ...,
convert_timedelta: bool = ...,
convert_period: Literal[True] = ...,
convert_interval: bool = ...,
convert_to_nullable_integer: bool = ...,
dtype_if_all_nat: DtypeObj | None = ...,
) -> ArrayLike: ...
Expand All @@ -125,6 +129,7 @@ def maybe_convert_objects(
convert_datetime: bool = ...,
convert_timedelta: bool = ...,
convert_period: bool = ...,
convert_interval: bool = ...,
convert_to_nullable_integer: bool = ...,
dtype_if_all_nat: DtypeObj | None = ...,
) -> ArrayLike: ...
Expand Down
17 changes: 17 additions & 0 deletions pandas/_libs/lib.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -1573,6 +1573,7 @@ def infer_datetimelike_array(arr: ndarray[object]) -> tuple[str, bool]:
bint seen_timedelta = False, seen_date = False, seen_datetime = False
bint seen_tz_aware = False, seen_tz_naive = False
bint seen_nat = False, seen_str = False
bint seen_period = False, seen_interval = False
list objs = []
object v

Expand Down Expand Up @@ -1610,9 +1611,25 @@ def infer_datetimelike_array(arr: ndarray[object]) -> tuple[str, bool]:
elif is_timedelta(v):
# timedelta, or timedelta64
seen_timedelta = True
elif is_period_object(v):
seen_period = True
break
elif is_interval(v):
seen_interval = True
break
else:
return "mixed", seen_str

if seen_period:
if is_period_array(arr):
return "period", seen_str
return "mixed", seen_str

if seen_interval:
if is_interval_array(arr):
return "interval", seen_str
return "mixed", seen_str

if seen_date and not (seen_datetime or seen_timedelta):
return "date", seen_str
elif seen_datetime and not seen_timedelta:
Expand Down
14 changes: 3 additions & 11 deletions pandas/core/construction.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,6 @@
is_integer_dtype,
is_list_like,
is_object_dtype,
is_string_dtype,
is_timedelta64_ns_dtype,
)
from pandas.core.dtypes.dtypes import DatetimeTZDtype
Expand Down Expand Up @@ -565,18 +564,11 @@ def sanitize_array(

subarr = _sanitize_ndim(subarr, data, dtype, index, allow_2d=allow_2d)

if not (
isinstance(subarr.dtype, ExtensionDtype) or isinstance(dtype, ExtensionDtype)
):
if isinstance(subarr, np.ndarray):
# at this point we should have dtype be None or subarr.dtype == dtype
dtype = cast(np.dtype, dtype)
subarr = _sanitize_str_dtypes(subarr, data, dtype, copy)

is_object_or_str_dtype = is_object_dtype(dtype) or is_string_dtype(dtype)
if is_object_dtype(subarr.dtype) and not is_object_or_str_dtype:
inferred = lib.infer_dtype(subarr, skipna=False)
if inferred in {"interval", "period"}:
subarr = array(subarr)
subarr = extract_array(subarr, extract_numpy=True)

return subarr


Expand Down
12 changes: 9 additions & 3 deletions pandas/core/dtypes/cast.py
Original file line number Diff line number Diff line change
Expand Up @@ -1464,7 +1464,7 @@ def convert_dtypes(

def maybe_infer_to_datetimelike(
value: np.ndarray,
) -> np.ndarray | DatetimeArray | TimedeltaArray:
) -> np.ndarray | DatetimeArray | TimedeltaArray | PeriodArray | IntervalArray:
"""
we might have a array (or single object) that is datetime like,
and no dtype is passed don't change the value unless we find a
Expand All @@ -1479,7 +1479,7 @@ def maybe_infer_to_datetimelike(

Returns
-------
np.ndarray, DatetimeArray, or TimedeltaArray
np.ndarray, DatetimeArray, TimedeltaArray, PeriodArray, or IntervalArray

"""
if not isinstance(value, np.ndarray) or value.dtype != object:
Expand Down Expand Up @@ -1528,6 +1528,13 @@ def try_timedelta(v: np.ndarray) -> np.ndarray:
return td_values.reshape(shape)

inferred_type, seen_str = lib.infer_datetimelike_array(ensure_object(v))
if inferred_type in ["period", "interval"]:
# Incompatible return value type (got "Union[ExtensionArray, ndarray]",
# expected "Union[ndarray, DatetimeArray, TimedeltaArray, PeriodArray,
# IntervalArray]")
return lib.maybe_convert_objects( # type: ignore[return-value]
v, convert_period=True, convert_interval=True
)

if inferred_type == "datetime":
# error: Incompatible types in assignment (expression has type "ExtensionArray",
Expand Down Expand Up @@ -1564,7 +1571,6 @@ def try_timedelta(v: np.ndarray) -> np.ndarray:
FutureWarning,
stacklevel=find_stack_level(),
)
# return v.reshape(shape)
return value


Expand Down
13 changes: 13 additions & 0 deletions pandas/tests/extension/base/reshaping.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,11 @@
import numpy as np
import pytest

from pandas.core.dtypes.common import (
is_interval_dtype,
is_period_dtype,
)

import pandas as pd
from pandas.api.extensions import ExtensionArray
from pandas.core.internals import ExtensionBlock
Expand Down Expand Up @@ -321,6 +326,14 @@ def test_unstack(self, data, index, obj):
expected = ser.astype(object).unstack(
level=level, fill_value=data.dtype.na_value
)
if obj == "series":
# TODO: special cases belong in dtype-specific tests
if is_period_dtype(data.dtype):
assert expected.dtypes.apply(is_period_dtype).all()
expected = expected.astype(object)
if is_interval_dtype(data.dtype):
assert expected.dtypes.apply(is_interval_dtype).all()
expected = expected.astype(object)
result = result.astype(object)

self.assert_frame_equal(result, expected)
Expand Down
22 changes: 22 additions & 0 deletions pandas/tests/frame/test_constructors.py
Original file line number Diff line number Diff line change
Expand Up @@ -2494,6 +2494,28 @@ def test_nested_list_columns(self):
)
tm.assert_frame_equal(result, expected)

def test_from_2d_object_array_of_periods_or_intervals(self):
# Period analogue to GH#26825
pi = pd.period_range("2016-04-05", periods=3)
data = pi._data.astype(object).reshape(1, -1)
df = DataFrame(data)
assert df.shape == (1, 3)
assert (df.dtypes == pi.dtype).all()
assert (df == pi).all().all()

ii = pd.IntervalIndex.from_breaks([3, 4, 5, 6])
data2 = ii._data.astype(object).reshape(1, -1)
df2 = DataFrame(data2)
assert df2.shape == (1, 3)
assert (df2.dtypes == ii.dtype).all()
assert (df2 == ii).all().all()

# mixed
data3 = np.r_[data, data2, data, data2].T
df3 = DataFrame(data3)
expected = DataFrame({0: pi, 1: ii, 2: pi, 3: ii})
tm.assert_frame_equal(df3, expected)


class TestDataFrameConstructorWithDtypeCoercion:
def test_floating_values_integer_dtype(self):
Expand Down
4 changes: 3 additions & 1 deletion pandas/tests/tools/test_to_timedelta.py
Original file line number Diff line number Diff line change
Expand Up @@ -208,7 +208,9 @@ def test_to_timedelta_on_missing_values(self):
tm.assert_series_equal(actual, expected)

with tm.assert_produces_warning(FutureWarning, match="Inferring timedelta64"):
actual = to_timedelta(Series(["00:00:01", pd.NaT]))
ser = Series(["00:00:01", pd.NaT])
assert ser.dtype == "m8[ns]"
actual = to_timedelta(ser)
tm.assert_series_equal(actual, expected)

actual = to_timedelta(np.nan)
Expand Down