Skip to content

Support time attribute access on PeriodIndex and PeriodArray - #11943

Open
mvanhorn wants to merge 2 commits into
pydata:mainfrom
mvanhorn:fix/1565-periodindex-time-attributes
Open

mvanhorn wants to merge 2 commits into
pydata:mainfrom
mvanhorn:fix/1565-periodindex-time-attributes

Conversation

@mvanhorn

Copy link
Copy Markdown
Contributor

Description

Reading the month from a period-valued time coordinate raises a TypeError through both da.time.dt.month and da["time.month"]. Grouping by time.month fails at the same lookup. The accessor and dotted coordinate lookup passed the pandas period dtype to NumPy's subtype checks. NumPy cannot interpret that dtype, so lookup failed before pandas could extract the requested field.

Both lookup paths now recognize PeriodDtype, and the NumPy subtype checks skip pandas extension dtypes. Field extraction and formatting preserve the period dtype when constructing a pandas Series from a PeriodIndex or PeriodArray. Periods use the proleptic Gregorian calendar, and missing periods are masked when extracting integer fields or deriving seasons. Regression tests cover period-valued data and coordinates, including dotted lookup and grouping.

pytest -q xarray/tests/test_accessor_dt.py xarray/tests/test_dataset.py stopped before test collection because NumPy was missing.
A run with the xarray _access_through_cftimeindex change reverted gave no clear result, so no test is shown to catch this bug.
The changed test xarray/tests/test_accessor_dt.py did not run here: the numpy module is not installed.
The changed test xarray/tests/test_dataset.py did not run here: the numpy module is not installed.

Closes #1565

Checklist

AI Disclosure

  • This PR contains AI-generated content.
    • I have tested any AI-generated content in my PR.
      No result recorded for the xarray _access_through_cftimeindex change backs this box.

    • I take responsibility for any AI-generated content in my PR.

      Tools: {e.g., Claude, Codex, GitHub Copilot, ChatGPT, etc.}

AI disclosure

AI was used for assistance.

  • Tools: Codex.

Both lookup paths now recognize `PeriodDtype`, and the NumPy subtype
checks skip pandas extension dtypes. Field extraction and formatting
preserve the period dtype when constructing a pandas Series from a
`PeriodIndex` or `PeriodArray`. Periods use the proleptic Gregorian
calendar, and missing periods are masked when extracting integer fields
or deriving seasons. Regression tests cover period-valued data and
coordinates, including dotted lookup and grouping.

Fixes pydata#1565

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Regression: time attributes on PeriodIndex

1 participant