From 4b9255cf3eb39355ae26840c359760df7a68a58a Mon Sep 17 00:00:00 2001 From: Rodrigo Cabrera Castaldoni Date: Sun, 17 Oct 2021 21:14:43 -0300 Subject: [PATCH 1/5] BUG coerce to numpy array --- pandas/core/tools/datetimes.py | 2 +- pandas/tests/tools/test_to_datetime.py | 20 +++++++++++++++++++- 2 files changed, 20 insertions(+), 2 deletions(-) diff --git a/pandas/core/tools/datetimes.py b/pandas/core/tools/datetimes.py index 8b37026e16171..7162cbcb29be7 100644 --- a/pandas/core/tools/datetimes.py +++ b/pandas/core/tools/datetimes.py @@ -325,7 +325,6 @@ def _convert_listlike_datetimes( ------- Index-like of parsed dates """ - if isinstance(arg, (list, tuple)): arg = np.array(arg, dtype="O") @@ -525,6 +524,7 @@ def _to_datetime_with_unit(arg, unit, name, tz, errors: str) -> Index: arr = arg.astype(f"datetime64[{unit}]") tz_parsed = None else: + arg = np.array(arg) arr, tz_parsed = tslib.array_with_unit_to_datetime(arg, unit, errors=errors) if errors == "ignore": diff --git a/pandas/tests/tools/test_to_datetime.py b/pandas/tests/tools/test_to_datetime.py index 850ce6df21b7f..673955cdf4dec 100644 --- a/pandas/tests/tools/test_to_datetime.py +++ b/pandas/tests/tools/test_to_datetime.py @@ -2640,9 +2640,27 @@ def test_empty_string_datetime_coerce__unit(): tm.assert_index_equal(expected, result) +@td.skip_if_no("xarray") +def test_xarray_coerce_unit(): + import xarray as xr + + arr = xr.DataArray([1, 2, 3]) + result = to_datetime(arr, unit="ns") + expected = DatetimeIndex( + [ + "1970-01-01 00:00:00.000000001", + "1970-01-01 00:00:00.000000002", + "1970-01-01 00:00:00.000000003", + ], + dtype="datetime64[ns]", + freq=None, + ) + tm.assert_index_equal(result, expected) + + @pytest.mark.parametrize("cache", [True, False]) def test_to_datetime_monotonic_increasing_index(cache): - # GH28238 + # 44053 cstart = start_caching_at times = date_range(Timestamp("1980"), periods=cstart, freq="YS") times = times.to_frame(index=False, name="DT").sample(n=cstart, random_state=1) From 2cc86c1e5310032ed7cd5313574fbf755214efc9 Mon Sep 17 00:00:00 2001 From: Rodrigo Cabrera Castaldoni Date: Sun, 17 Oct 2021 21:45:28 -0300 Subject: [PATCH 2/5] BUG: coerce to numpy array --- pandas/core/tools/datetimes.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/pandas/core/tools/datetimes.py b/pandas/core/tools/datetimes.py index 7162cbcb29be7..9fad787996d31 100644 --- a/pandas/core/tools/datetimes.py +++ b/pandas/core/tools/datetimes.py @@ -524,9 +524,9 @@ def _to_datetime_with_unit(arg, unit, name, tz, errors: str) -> Index: arr = arg.astype(f"datetime64[{unit}]") tz_parsed = None else: - arg = np.array(arg) - arr, tz_parsed = tslib.array_with_unit_to_datetime(arg, unit, errors=errors) - + arg = np.asarray(arg) + arr, tz_parsed = tslib.array_with_unit_to_datetime(arg, unit, errors=errors) + if errors == "ignore": # Index constructor _may_ infer to DatetimeIndex result = Index._with_infer(arr, name=name) From 3b1d01f08da0d700009d2db31f554a7804bb135c Mon Sep 17 00:00:00 2001 From: Rodrigo Cabrera Castaldoni Date: Sun, 17 Oct 2021 21:47:23 -0300 Subject: [PATCH 3/5] BUG: coerce to numpy array --- pandas/core/tools/datetimes.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/pandas/core/tools/datetimes.py b/pandas/core/tools/datetimes.py index 9fad787996d31..10be942a49d15 100644 --- a/pandas/core/tools/datetimes.py +++ b/pandas/core/tools/datetimes.py @@ -524,9 +524,9 @@ def _to_datetime_with_unit(arg, unit, name, tz, errors: str) -> Index: arr = arg.astype(f"datetime64[{unit}]") tz_parsed = None else: - arg = np.asarray(arg) - arr, tz_parsed = tslib.array_with_unit_to_datetime(arg, unit, errors=errors) - + arg = np.asarray(arg) + arr, tz_parsed = tslib.array_with_unit_to_datetime(arg, unit, errors=errors) + if errors == "ignore": # Index constructor _may_ infer to DatetimeIndex result = Index._with_infer(arr, name=name) From fb484e9e47056c576cc4734bb562b5a037a38596 Mon Sep 17 00:00:00 2001 From: Rodrigo Cabrera Castaldoni Date: Sun, 17 Oct 2021 22:59:07 -0300 Subject: [PATCH 4/5] Changing to correct code --- pandas/tests/tools/test_to_datetime.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pandas/tests/tools/test_to_datetime.py b/pandas/tests/tools/test_to_datetime.py index 673955cdf4dec..3d3e2d766abd0 100644 --- a/pandas/tests/tools/test_to_datetime.py +++ b/pandas/tests/tools/test_to_datetime.py @@ -2660,7 +2660,7 @@ def test_xarray_coerce_unit(): @pytest.mark.parametrize("cache", [True, False]) def test_to_datetime_monotonic_increasing_index(cache): - # 44053 + # GH28238 cstart = start_caching_at times = date_range(Timestamp("1980"), periods=cstart, freq="YS") times = times.to_frame(index=False, name="DT").sample(n=cstart, random_state=1) From dc17aeb05787785efdbc81346bfa17fc6ee9ffac Mon Sep 17 00:00:00 2001 From: Rodrigo Cabrera Castaldoni Date: Mon, 18 Oct 2021 20:39:15 -0300 Subject: [PATCH 5/5] BUG: to_datetime with xarray DataArray and specifie unit errors --- doc/source/whatsnew/v1.4.0.rst | 1 + pandas/tests/tools/test_to_datetime.py | 1 + 2 files changed, 2 insertions(+) diff --git a/doc/source/whatsnew/v1.4.0.rst b/doc/source/whatsnew/v1.4.0.rst index 964f4b83866c9..4465875fc9ae6 100644 --- a/doc/source/whatsnew/v1.4.0.rst +++ b/doc/source/whatsnew/v1.4.0.rst @@ -467,6 +467,7 @@ Conversion ^^^^^^^^^^ - Bug in :class:`UInt64Index` constructor when passing a list containing both positive integers small enough to cast to int64 and integers too large too hold in int64 (:issue:`42201`) - Bug in :class:`Series` constructor returning 0 for missing values with dtype ``int64`` and ``False`` for dtype ``bool`` (:issue:`43017`, :issue:`43018`) +- Bug in :func:`to_datetime` with ``arg:xr.DataArray`` and ``unit="ns"`` specified raises TypeError (:issue:`44053`) - Strings diff --git a/pandas/tests/tools/test_to_datetime.py b/pandas/tests/tools/test_to_datetime.py index 3d3e2d766abd0..3fa6441e47242 100644 --- a/pandas/tests/tools/test_to_datetime.py +++ b/pandas/tests/tools/test_to_datetime.py @@ -2642,6 +2642,7 @@ def test_empty_string_datetime_coerce__unit(): @td.skip_if_no("xarray") def test_xarray_coerce_unit(): + # GH44053 import xarray as xr arr = xr.DataArray([1, 2, 3])