|
132 | 132 | from pandas.core.construction import extract_array |
133 | 133 | from pandas.core.generic import NDFrame, _shared_docs |
134 | 134 | from pandas.core.indexes import base as ibase |
135 | | -from pandas.core.indexes.api import Index, ensure_index, ensure_index_from_sequences |
| 135 | +from pandas.core.indexes.api import ( |
| 136 | + DatetimeIndex, |
| 137 | + Index, |
| 138 | + PeriodIndex, |
| 139 | + ensure_index, |
| 140 | + ensure_index_from_sequences, |
| 141 | +) |
136 | 142 | from pandas.core.indexes.multi import MultiIndex, maybe_droplevels |
137 | 143 | from pandas.core.indexing import check_bool_indexer, convert_to_index_sliceable |
138 | 144 | from pandas.core.internals import BlockManager |
@@ -9254,6 +9260,9 @@ def to_timestamp( |
9254 | 9260 |
|
9255 | 9261 | axis_name = self._get_axis_name(axis) |
9256 | 9262 | old_ax = getattr(self, axis_name) |
| 9263 | + if not isinstance(old_ax, PeriodIndex): |
| 9264 | + raise TypeError(f"unsupported Type {type(old_ax).__name__}") |
| 9265 | + |
9257 | 9266 | new_ax = old_ax.to_timestamp(freq=freq, how=how) |
9258 | 9267 |
|
9259 | 9268 | setattr(new_obj, axis_name, new_ax) |
@@ -9283,6 +9292,9 @@ def to_period(self, freq=None, axis: Axis = 0, copy: bool = True) -> DataFrame: |
9283 | 9292 |
|
9284 | 9293 | axis_name = self._get_axis_name(axis) |
9285 | 9294 | old_ax = getattr(self, axis_name) |
| 9295 | + if not isinstance(old_ax, DatetimeIndex): |
| 9296 | + raise TypeError(f"unsupported Type {type(old_ax).__name__}") |
| 9297 | + |
9286 | 9298 | new_ax = old_ax.to_period(freq=freq) |
9287 | 9299 |
|
9288 | 9300 | setattr(new_obj, axis_name, new_ax) |
|
0 commit comments