@@ -977,6 +977,23 @@ def to_timestamp(self, freq=None, how: str = "start") -> DatetimeArray:
977977 new_data = libperiod .periodarr_to_dt64arr (new_parr .asi8 , base )
978978 dta = DatetimeArray ._from_sequence (new_data , dtype = new_data .dtype )
979979 assert dta .unit == unit
980+ return dta
981+
982+ def _to_timestamp_freq (
983+ self , dta : DatetimeArray , target_freq , how : str
984+ ) -> BaseOffset | None :
985+ """
986+ Determine the freq to stamp on the DatetimeArray returned by
987+ ``self.to_timestamp(target_freq, how)``.
988+
989+ Called by ``PeriodIndex.to_timestamp`` so the array-level
990+ ``to_timestamp`` can return a bare DatetimeArray.
991+ """
992+ how = libperiod .validate_end_alias (how )
993+ if how == "E" :
994+ # For the "end" case, to_timestamp routes through arithmetic that
995+ # does not preserve freq; match that behavior here.
996+ return None
980997
981998 if self .freq .rule_code == "B" :
982999 # See if we can retain BDay instead of Day in cases where
@@ -985,22 +1002,23 @@ def to_timestamp(self, freq=None, how: str = "start") -> DatetimeArray:
9851002 if len (diffs ) == 1 :
9861003 diff = diffs [0 ]
9871004 if diff == self .dtype ._n :
988- dta . _freq = self .freq
1005+ return self .freq
9891006 elif diff == 1 :
990- dta . _freq = self .freq .base
1007+ return self .freq .base
9911008 # TODO: other cases?
992- return dta
993- else :
994- dta ._freq = to_offset (dta .inferred_freq )
995- if freq is not None :
996- freq = to_offset (freq )
997- if (
998- isinstance (dta .freq , Day )
999- and not isinstance (freq , Day )
1000- and Timedelta (freq ) == Timedelta (days = dta .freq .n )
1001- ):
1002- dta ._freq = freq
1003- return dta
1009+ return None
1010+
1011+ result_freq = to_offset (dta .inferred_freq )
1012+ if target_freq is not None :
1013+ # match the normalization applied in to_timestamp
1014+ target_freq = Period ._maybe_convert_freq (target_freq )
1015+ if (
1016+ isinstance (result_freq , Day )
1017+ and not isinstance (target_freq , Day )
1018+ and Timedelta (target_freq ) == Timedelta (days = result_freq .n )
1019+ ):
1020+ result_freq = target_freq
1021+ return result_freq
10041022
10051023 # --------------------------------------------------------------------
10061024
0 commit comments