Skip to content

BUG: dt64.astype(period) allowed but not vice-versa #45038

Closed
@jbrockmendel

Description

@jbrockmendel
import pandas as pd

dti = pd.date_range("2016-01-01", periods=3)
pi = dti.to_period("D")

dti.astype(pi.dtype)  # <- OK
pi.astype(dti.dtype)  # <- works but deprecated in #44398

pd.Series(dti).astype(pi.dtype)  # <- OK
pd.Series(pi).astype(dti.dtype)  # <- raises

In #44398 we deprecated PeriodIndex.astype(dt64). The motivation was that PeriodIndex.astype should be consistent with Series[Period].astype. I chose to deprecate the PeriodIndex behavior instead of allow it in Series.astype bc there's an optional keyword in PeriodIndex.astype that we're deprecating anyway.

I'm having seconds thoughts based on the fact that we do support Series[dt64].astype(period), and it seems like these should be able to round-trip.

Metadata

Metadata

Assignees

No one assigned

    Labels

    API - ConsistencyInternal Consistency of API/BehaviorDtype ConversionsUnexpected or buggy dtype conversionsPeriodPeriod data type

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions