Skip to content

BUG: setitem into td64/dt64 series/frame with Categorical[strings] #44236

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
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
2 changes: 1 addition & 1 deletion doc/source/whatsnew/v1.4.0.rst
Original file line number Diff line number Diff line change
Expand Up @@ -535,7 +535,7 @@ Indexing
- Bug in :meth:`DataFrame.sort_index` where ``ignore_index=True`` was not being respected when the index was already sorted (:issue:`43591`)
- Bug in :meth:`Index.get_indexer_non_unique` when index contains multiple ``np.datetime64("NaT")`` and ``np.timedelta64("NaT")`` (:issue:`43869`)
- Bug in setting a scalar :class:`Interval` value into a :class:`Series` with ``IntervalDtype`` when the scalar's sides are floats and the values' sides are integers (:issue:`44201`)
-
- Bug when setting string-backed :class:`Categorical` values that can be parsed to datetimes into a :class:`DatetimeArray` or :class:`Series` or :class:`DataFrame` column backed by :class:`DatetimeArray` failing to parse these strings (:issue:`44236`)


Missing
Expand Down
3 changes: 2 additions & 1 deletion pandas/core/arrays/datetimelike.py
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@
from pandas.util._exceptions import find_stack_level

from pandas.core.dtypes.common import (
is_all_strings,
is_categorical_dtype,
is_datetime64_any_dtype,
is_datetime64_dtype,
Expand Down Expand Up @@ -720,7 +721,7 @@ def _validate_listlike(self, value, allow_object: bool = False):
value = pd_array(value)
value = extract_array(value, extract_numpy=True)

if is_dtype_equal(value.dtype, "string"):
if is_all_strings(value):
# We got a StringArray
try:
# TODO: Could use from_sequence_of_strings if implemented
Expand Down
21 changes: 21 additions & 0 deletions pandas/core/dtypes/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
Interval,
Period,
algos,
lib,
)
from pandas._libs.tslibs import conversion
from pandas._typing import (
Expand Down Expand Up @@ -1788,3 +1789,23 @@ def pandas_dtype(dtype) -> DtypeObj:
raise TypeError(f"dtype '{dtype}' not understood")

return npdtype


def is_all_strings(value: ArrayLike) -> bool:
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

wouldn't hurt to add some unit tests for this when you have a chance :->

"""
Check if this is an array of strings that we should try parsing.

Includes object-dtype ndarray containing all-strings, StringArray,
and Categorical with all-string categories.
Does not include numpy string dtypes.
"""
dtype = value.dtype

if isinstance(dtype, np.dtype):
return (
dtype == np.dtype("object")
and lib.infer_dtype(value, skipna=False) == "string"
)
elif isinstance(dtype, CategoricalDtype):
return dtype.categories.inferred_type == "string"
return dtype == "string"
4 changes: 2 additions & 2 deletions pandas/tests/indexing/test_indexing.py
Original file line number Diff line number Diff line change
Expand Up @@ -871,7 +871,7 @@ def test_setitem_dt64_string_scalar(self, tz_naive_fixture, indexer_sli):
else:
assert ser._values is values

@pytest.mark.parametrize("box", [list, np.array, pd.array])
@pytest.mark.parametrize("box", [list, np.array, pd.array, pd.Categorical, Index])
@pytest.mark.parametrize(
"key", [[0, 1], slice(0, 2), np.array([True, True, False])]
)
Expand Down Expand Up @@ -911,7 +911,7 @@ def test_setitem_td64_scalar(self, indexer_sli, scalar):
indexer_sli(ser)[0] = scalar
assert ser._values._data is values._data

@pytest.mark.parametrize("box", [list, np.array, pd.array])
@pytest.mark.parametrize("box", [list, np.array, pd.array, pd.Categorical, Index])
@pytest.mark.parametrize(
"key", [[0, 1], slice(0, 2), np.array([True, True, False])]
)
Expand Down