13
13
import numpy as np
14
14
import pandas as pd
15
15
from numpy .typing import ArrayLike
16
+ from packaging .version import Version
16
17
17
18
import xarray as xr # only for Dataset and DataArray
18
19
from xarray .compat .array_api_compat import to_like_array
@@ -208,7 +209,10 @@ def _maybe_wrap_data(data):
208
209
209
210
def _possibly_convert_objects (values ):
210
211
"""Convert object arrays into datetime64 and timedelta64 according
211
- to the pandas convention.
212
+ to the pandas convention. For backwards compat, as of 3.0.0 pandas,
213
+ object dtype inputs are cast to strings by `pandas.Series`
214
+ but we output them as object dtype with the input metadata preserved as well.
215
+
212
216
213
217
* datetime.datetime
214
218
* datetime.timedelta
@@ -223,6 +227,17 @@ def _possibly_convert_objects(values):
223
227
result .flags .writeable = True
224
228
except ValueError :
225
229
result = result .copy ()
230
+ # For why we need this behavior: https://github.com/pandas-dev/pandas/issues/61938
231
+ # Object datatype inputs that are strings
232
+ # will be converted to strings by `pandas.Series`, and as of 3.0.0, lose
233
+ # `dtype.metadata`. If the roundtrip back to numpy in this function yields an
234
+ # object array again, the dtype.metadata will be preserved.
235
+ if (
236
+ result .dtype .kind == "O"
237
+ and values .dtype .kind == "O"
238
+ and Version (pd .__version__ ) >= Version ("3.0.0dev0" )
239
+ ):
240
+ result .dtype = values .dtype
226
241
return result
227
242
228
243
0 commit comments