Description
What happened?
Calling the weekday_name
such as:
da.dt.weekday_name
raises an exception.
Looking into it more deeply, i.e. xarray source it can be seen the values are converted to a Pandas Series and the attribute called directly from that series.
This attribute was removed from Pandas in v1.0.0
Removed Timestamp.weekday_name, DatetimeIndex.weekday_name, and Series.dt.weekday_name (GH 18164)
It was replaced with the method day_name() - so the code would need to consider this case separately.
What did you expect to happen?
Return the weekday_name.
Minimal Complete Verifiable Example
import pandas as pd
import xarray as xr
da = xr.DataArray(pd.date_range("2024-01-15", periods=2))
day_name = da.dt.weekday_name
MVCE confirmation
- Minimal example — the example is as focused as reasonably possible to demonstrate the underlying issue in xarray.
- Complete example — the example is self-contained, including all data and the text of any traceback.
- Verifiable example — the example copy & pastes into an IPython prompt or Binder notebook, returning the result.
- New issue — a search of GitHub Issues suggests this is not a duplicate.
- Recent environment — the issue occurs with the latest version of xarray and its dependencies.
Relevant log output
Traceback (most recent call last):
File "<string>", line 1, in <module>
File "/Users/sam/Documents/GitHub/filebasedbim/venv/lib/python3.12/site-packages/xarray/core/accessor_dt.py", line 462, in weekday_name
return self._date_field("weekday_name", object)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/Users/sam/Documents/GitHub/filebasedbim/venv/lib/python3.12/site-packages/xarray/core/accessor_dt.py", line 245, in _date_field
result = _get_date_field(_index_or_data(self._obj), name, dtype)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/Users/sam/Documents/GitHub/filebasedbim/venv/lib/python3.12/site-packages/xarray/core/accessor_dt.py", line 139, in _get_date_field
out = access_method(values, name)
^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/Users/sam/Documents/GitHub/filebasedbim/venv/lib/python3.12/site-packages/xarray/core/accessor_dt.py", line 97, in _access_through_series
field_values = getattr(values_as_series.dt, name).values
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
AttributeError: 'DatetimeProperties' object has no attribute 'weekday_name'
Anything else we need to know?
day_name() accepts a locale, and if not provided, defaults to 'en_US.utf8'.
Pandas also have the month_name() method which may be worth implementing at the same time.
Alternatively, if locale implementation is not desired, weekday_name should be depreciated and removed as Pandas > v1.5 is required.
Environment
INSTALLED VERSIONS
commit: None
python: 3.12.0 (main, Oct 5 2023, 15:52:37) [Clang 14.0.3 (clang-1403.0.22.14.1)]
python-bits: 64
OS: Darwin
OS-release: 22.6.0
machine: x86_64
processor: i386
byteorder: little
LC_ALL: None
LANG: None
LOCALE: (None, 'UTF-8')
libhdf5: None
libnetcdf: None
xarray: 2023.12.0
pandas: 2.1.4
numpy: 1.26.2
scipy: None
netCDF4: None
pydap: None
h5netcdf: None
h5py: None
Nio: None
zarr: None
cftime: None
nc_time_axis: None
iris: None
bottleneck: None
dask: None
distributed: None
matplotlib: None
cartopy: None
seaborn: None
numbagg: None
fsspec: None
cupy: None
pint: None
sparse: None
flox: None
numpy_groupies: None
setuptools: 68.2.2
pip: 23.3.1
conda: None
pytest: 7.4.3
mypy: None
IPython: None
sphinx: None