Skip to content

Commit 41df89d

Browse files
committed
Reflect reviews
- update doc - update dtype matching fucntion - update test
1 parent 9efb169 commit 41df89d

File tree

3 files changed

+22
-3
lines changed

3 files changed

+22
-3
lines changed

doc/source/whatsnew/v0.25.1.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,6 @@ Categorical
3232
Datetimelike
3333
^^^^^^^^^^^^
3434
- Bug in :func:`to_datetime` where passing a timezone-naive :class:`DatetimeArray` or :class:`DatetimeIndex` and ``utc=True`` would incorrectly return a timezone-naive result (:issue:`27733`)
35-
- Bug in :func:`maybe_cast_to_datetime` where converting a `np.datetime64` to `datetime64[D]` raise `TypeError` (:issue: `27395`)
3635
-
3736
-
3837

@@ -85,6 +84,7 @@ Indexing
8584
- Bug in partial-string indexing returning a NumPy array rather than a ``Series`` when indexing with a scalar like ``.loc['2015']`` (:issue:`27516`)
8685
- Break reference cycle involving :class:`Index` and other index classes to allow garbage collection of index objects without running the GC. (:issue:`27585`, :issue:`27840`)
8786
- Fix regression in assigning values to a single column of a DataFrame with a ``MultiIndex`` columns (:issue:`27841`).
87+
- Fix assignment of column via `.loc` with numpy `non-ns datetime` type (:issue: `27395`)
8888
-
8989

9090
Missing

pandas/core/dtypes/cast.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1026,7 +1026,7 @@ def maybe_cast_to_datetime(value, dtype, errors="raise"):
10261026
)
10271027

10281028
if is_datetime64 and not is_dtype_equal(dtype, _NS_DTYPE):
1029-
if dtype.name in ("datetime64", "datetime64[ns]", "datetime64[D]"):
1029+
if dtype.kind == "M":
10301030
if dtype.name == "datetime64":
10311031
raise ValueError(msg.format(dtype=dtype.name))
10321032
dtype = _NS_DTYPE
@@ -1044,7 +1044,7 @@ def maybe_cast_to_datetime(value, dtype, errors="raise"):
10441044
value = [value]
10451045

10461046
elif is_timedelta64 and not is_dtype_equal(dtype, _TD_DTYPE):
1047-
if dtype.name in ("timedelta64", "timedelta64[ns]", "timedelta64[D]"):
1047+
if dtype.kind == "m":
10481048
if dtype.name == "timedelta64":
10491049
raise ValueError(msg.format(dtype=dtype.name))
10501050
dtype = _TD_DTYPE

pandas/tests/indexing/test_loc.py

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
import pandas as pd
1010
from pandas import DataFrame, Series, Timestamp, date_range
1111
from pandas.api.types import is_scalar
12+
from pandas.core.dtypes.cast import maybe_cast_to_datetime
1213
from pandas.tests.indexing.common import Base
1314
from pandas.util import testing as tm
1415

@@ -690,6 +691,24 @@ def test_loc_setitem_consistency_slice_column_len(self):
690691
)
691692
tm.assert_series_equal(df[("Respondent", "Duration")], expected)
692693

694+
@pytest.mark.parametrize(
695+
"obj,dtype",
696+
[
697+
(np.datetime64("2017-01-01 01:00:00"), "datetime64"),
698+
(np.datetime64("2017-01-01 02:00:00"), "datetime64[ns]"),
699+
(np.datetime64("2017-01-02 01:00:00"), "datetime64[Y]"),
700+
(np.datetime64("2017-01-03 02:00:00"), "datetime64[M]"),
701+
(np.datetime64("2017-01-04 02:00:00"), "datetime64[D]"),
702+
(np.datetime64("2017-01-05 02:00:00"), "datetime64[h]"),
703+
(np.datetime64("2017-01-06 02:10:00"), "datetime64[m]"),
704+
(np.datetime64("2017-01-07 02:20:10"), "datetime64[s]"),
705+
(np.datetime64("2017-01-08 02:40:20"), "datetime64[ms]"),
706+
(np.datetime64("2017-01-09 02:50:30"), "datetime64[ns]"),
707+
],
708+
)
709+
def test_maybe_cast_to_datetime(obj, dtype):
710+
maybe_cast_to_datetime(obj, dtype)
711+
693712
def test_loc_setitem_frame(self):
694713
df = self.frame_labels
695714

0 commit comments

Comments
 (0)