Skip to content

Commit

Permalink
BUG: to_datetime with xarray DataArray and specifie unit errors (#44074)
Browse files Browse the repository at this point in the history
  • Loading branch information
rocabrera authored Oct 19, 2021
1 parent f2abbd9 commit 3a6d4cd
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 1 deletion.
1 change: 1 addition & 0 deletions doc/source/whatsnew/v1.4.0.rst
Original file line number Diff line number Diff line change
Expand Up @@ -470,6 +470,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
Expand Down
2 changes: 1 addition & 1 deletion pandas/core/tools/datetimes.py
Original file line number Diff line number Diff line change
Expand Up @@ -325,7 +325,6 @@ def _convert_listlike_datetimes(
-------
Index-like of parsed dates
"""

if isinstance(arg, (list, tuple)):
arg = np.array(arg, dtype="O")

Expand Down Expand Up @@ -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.asarray(arg)
arr, tz_parsed = tslib.array_with_unit_to_datetime(arg, unit, errors=errors)

if errors == "ignore":
Expand Down
19 changes: 19 additions & 0 deletions pandas/tests/tools/test_to_datetime.py
Original file line number Diff line number Diff line change
Expand Up @@ -2640,6 +2640,25 @@ def test_empty_string_datetime_coerce__unit():
tm.assert_index_equal(expected, result)


@td.skip_if_no("xarray")
def test_xarray_coerce_unit():
# GH44053
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
Expand Down

0 comments on commit 3a6d4cd

Please sign in to comment.