MCPcopy
hub / github.com/pydata/xarray / _possibly_convert_objects

Function _possibly_convert_objects

xarray/core/variable.py:211–242  ·  view source on GitHub ↗

Convert object arrays into datetime64 and timedelta64 according to the pandas convention. For backwards compat, as of 3.0.0 pandas, object dtype inputs are cast to strings by `pandas.Series` but we output them as object dtype with the input metadata preserved as well. * datetime.d

(values)

Source from the content-addressed store, hash-verified

209
210
211def _possibly_convert_objects(values):
212 """Convert object arrays into datetime64 and timedelta64 according
213 to the pandas convention. For backwards compat, as of 3.0.0 pandas,
214 object dtype inputs are cast to strings by `pandas.Series`
215 but we output them as object dtype with the input metadata preserved as well.
216
217
218 * datetime.datetime
219 * datetime.timedelta
220 * pd.Timestamp
221 * pd.Timedelta
222 """
223 as_series = pd.Series(values.ravel(), copy=False)
224 result = np.asarray(as_series).reshape(values.shape)
225 if not result.flags.writeable:
226 # GH8843, pandas copy-on-write mode creates read-only arrays by default
227 try:
228 result.flags.writeable = True
229 except ValueError:
230 result = result.copy()
231 # For why we need this behavior: https://github.com/pandas-dev/pandas/issues/61938
232 # Object datatype inputs that are strings
233 # will be converted to strings by `pandas.Series`, and as of 3.0.0, lose
234 # `dtype.metadata`. If the roundtrip back to numpy in this function yields an
235 # object array again, the dtype.metadata will be preserved.
236 if (
237 result.dtype.kind == "O"
238 and values.dtype.kind == "O"
239 and Version(pd.__version__) >= Version("3.0.0dev0")
240 ):
241 result = result.view(values.dtype)
242 return result
243
244
245def as_compatible_data(

Callers 1

as_compatible_dataFunction · 0.85

Calls 1

copyMethod · 0.45

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…