Skip to content

Commit 678b3a7

Browse files
committed
ENH: unique/factorize preserve non-nano
1 parent 73d15a7 commit 678b3a7

File tree

3 files changed

+4
-16
lines changed

3 files changed

+4
-16
lines changed

doc/source/whatsnew/v1.6.0.rst

+1-1
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,7 @@ See :ref:`install.dependencies` and :ref:`install.optional_dependencies` for mor
115115

116116
Other API changes
117117
^^^^^^^^^^^^^^^^^
118-
-
118+
- :func:`factorize` and :func:`unique` preserve the original dtype when passed numpy timedelta64 or datetime64 with non-nanosecond resolution (:issue:`??`)
119119
-
120120

121121
.. ---------------------------------------------------------------------------

pandas/core/algorithms.py

-10
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,6 @@
4141
from pandas.core.dtypes.cast import (
4242
construct_1d_object_array_from_listlike,
4343
infer_dtype_from_array,
44-
sanitize_to_nanoseconds,
4544
)
4645
from pandas.core.dtypes.common import (
4746
ensure_float64,
@@ -51,7 +50,6 @@
5150
is_bool_dtype,
5251
is_categorical_dtype,
5352
is_complex_dtype,
54-
is_datetime64_dtype,
5553
is_extension_array_dtype,
5654
is_float_dtype,
5755
is_integer,
@@ -61,7 +59,6 @@
6159
is_object_dtype,
6260
is_scalar,
6361
is_signed_integer_dtype,
64-
is_timedelta64_dtype,
6562
needs_i8_conversion,
6663
)
6764
from pandas.core.dtypes.concat import concat_compat
@@ -184,8 +181,6 @@ def _ensure_data(values: ArrayLike) -> np.ndarray:
184181

185182
# datetimelike
186183
elif needs_i8_conversion(values.dtype):
187-
if isinstance(values, np.ndarray):
188-
values = sanitize_to_nanoseconds(values)
189184
npvalues = values.view("i8")
190185
npvalues = cast(np.ndarray, npvalues)
191186
return npvalues
@@ -223,11 +218,6 @@ def _reconstruct_data(
223218
values = cls._from_sequence(values, dtype=dtype)
224219

225220
else:
226-
if is_datetime64_dtype(dtype):
227-
dtype = np.dtype("datetime64[ns]")
228-
elif is_timedelta64_dtype(dtype):
229-
dtype = np.dtype("timedelta64[ns]")
230-
231221
values = values.astype(dtype, copy=False)
232222

233223
return values

pandas/tests/test_algos.py

+3-5
Original file line numberDiff line numberDiff line change
@@ -342,9 +342,7 @@ def test_datetime64_factorize(self, writable):
342342
data = np.array([np.datetime64("2020-01-01T00:00:00.000")])
343343
data.setflags(write=writable)
344344
expected_codes = np.array([0], dtype=np.intp)
345-
expected_uniques = np.array(
346-
["2020-01-01T00:00:00.000000000"], dtype="datetime64[ns]"
347-
)
345+
expected_uniques = np.array(["2020-01-01T00:00:00.000"], dtype="datetime64[ms]")
348346

349347
codes, uniques = pd.factorize(data)
350348
tm.assert_numpy_array_equal(codes, expected_codes)
@@ -609,13 +607,13 @@ def test_datetime64_dtype_array_returned(self):
609607
def test_datetime_non_ns(self):
610608
a = np.array(["2000", "2000", "2001"], dtype="datetime64[s]")
611609
result = pd.unique(a)
612-
expected = np.array(["2000", "2001"], dtype="datetime64[ns]")
610+
expected = a[1:]
613611
tm.assert_numpy_array_equal(result, expected)
614612

615613
def test_timedelta_non_ns(self):
616614
a = np.array(["2000", "2000", "2001"], dtype="timedelta64[s]")
617615
result = pd.unique(a)
618-
expected = np.array([2000000000000, 2001000000000], dtype="timedelta64[ns]")
616+
expected = a[1:]
619617
tm.assert_numpy_array_equal(result, expected)
620618

621619
def test_timedelta64_dtype_array_returned(self):

0 commit comments

Comments
 (0)