Skip to content

BUG: to_datetime with xarray DataArray and specifie unit errors #44073

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

Closed
wants to merge 37 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
37 commits
Select commit Hold shift + click to select a range
b534f56
Implemented the byte_string
shubham11941140 Aug 24, 2021
23583fc
Update test_convert_dtypes.py
shubham11941140 Aug 24, 2021
96338fa
Update test_convert_dtypes.py
shubham11941140 Aug 24, 2021
ad4189f
Update cast.py
shubham11941140 Aug 24, 2021
e915bc0
Passes the tests
shubham11941140 Aug 25, 2021
6d0a497
Removed PEP 8 issues
shubham11941140 Aug 25, 2021
5ab2d79
Update cast.py
shubham11941140 Aug 25, 2021
7921c6f
Removed Try Except Block
shubham11941140 Aug 29, 2021
3e7a91f
pre-commit changes added
shubham11941140 Aug 29, 2021
b5b0e27
mypy static error solved
shubham11941140 Aug 30, 2021
694fb7a
Updated as requested
shubham11941140 Aug 30, 2021
e12fd22
PEP-8 issues.
shubham11941140 Aug 30, 2021
b774cef
Update v1.3.3.rst - release note added
shubham11941140 Aug 30, 2021
f867f72
elif implemented
shubham11941140 Aug 30, 2021
0cb3f08
Changes done
shubham11941140 Sep 2, 2021
9760857
Merge branch 'master' into b2
shubham11941140 Sep 9, 2021
2e18ebc
Changes done
shubham11941140 Sep 9, 2021
3193de9
Update test_convert_dtypes.py
shubham11941140 Sep 9, 2021
20f8dda
Removed pre-commit error
shubham11941140 Sep 9, 2021
1503730
Update test_convert_dtypes.py
shubham11941140 Sep 9, 2021
c081d92
Added astype
shubham11941140 Sep 9, 2021
7654729
Compare bytes
shubham11941140 Sep 10, 2021
0a2e0a9
Remove build error
shubham11941140 Sep 11, 2021
d4e086c
Merge branch 'master' of https://github.com/pandas-dev/pandas into b2
shubham11941140 Sep 13, 2021
766a30f
Update 1.3.4.rst and removed 1.3.3.rst
shubham11941140 Sep 13, 2021
52ac57a
Merge branch 'master' into b2
shubham11941140 Sep 15, 2021
be03f37
Remove build error
shubham11941140 Sep 15, 2021
92677cc
Merge branch 'b2' of https://github.com/shubham11941140/pandas into b2
shubham11941140 Sep 15, 2021
07e1de9
Changed df
shubham11941140 Sep 16, 2021
5f2933d
Merge branch 'master' into b2
shubham11941140 Oct 1, 2021
bfb1242
Follows 1.2.5 behaviour
shubham11941140 Oct 16, 2021
37ee298
Merge branch 'b2' of https://github.com/shubham11941140/pandas into b2
shubham11941140 Oct 16, 2021
77ee435
Removed Whitespace
shubham11941140 Oct 16, 2021
e70f68b
Removed elif
shubham11941140 Oct 17, 2021
4560642
Merge branch 'master' of https://github.com/pandas-dev/pandas into b2
shubham11941140 Oct 17, 2021
35c0aee
xarray DataArray
shubham11941140 Oct 17, 2021
155563f
Added unit and removed PEP
shubham11941140 Oct 17, 2021
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
10 changes: 9 additions & 1 deletion pandas/core/tools/datetimes.py
Original file line number Diff line number Diff line change
Expand Up @@ -525,7 +525,15 @@ def _to_datetime_with_unit(arg, unit, name, tz, errors: str) -> Index:
arr = arg.astype(f"datetime64[{unit}]")
tz_parsed = None
else:
arr, tz_parsed = tslib.array_with_unit_to_datetime(arg, unit, errors=errors)
import xarray
Copy link
Contributor

Choose a reason for hiding this comment

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

pls don't import xarray like this
it's not a dependency

Copy link
Contributor Author

Choose a reason for hiding this comment

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

How do I do it? Should I write it on top?

if type(arg) is xarray.core.dataarray.DataArray:
val = arg.values
if type(val) == np.ndarray:
arr, tz_parsed = tslib.array_with_unit_to_datetime(
val, unit, errors=errors
)
else:
arr, tz_parsed = tslib.array_with_unit_to_datetime(arg, unit, errors=errors)

if errors == "ignore":
# Index constructor _may_ infer to DatetimeIndex
Expand Down
12 changes: 12 additions & 0 deletions pandas/tests/tools/test_to_datetime.py
Original file line number Diff line number Diff line change
Expand Up @@ -2650,3 +2650,15 @@ def test_to_datetime_monotonic_increasing_index(cache):
result = to_datetime(times.iloc[:, 0], cache=cache)
expected = times.iloc[:, 0]
tm.assert_series_equal(result, expected)


def test_xarray_DataArray():
Copy link
Contributor

Choose a reason for hiding this comment

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

you will need to skip if xarray is not installed

#GH44073
import xarray as xr
arr = xr.DataArray([1, 2, 3])
res = pd.to_datetime(arr, unit='ns')
ex = 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(ex, res)