Convert DataFrame from DatetimeIndex to PeriodIndex. Convert DataFrame from DatetimeIndex to PeriodIndex with desired frequency (inferred from index if not passed). Either index of columns can be converted, depending on `axis` argument. Parameters -
(
self,
freq: Frequency | None = None,
axis: Axis = 0,
copy: bool | lib.NoDefault = lib.no_default,
)
| 16297 | return new_obj |
| 16298 | |
| 16299 | def to_period( |
| 16300 | self, |
| 16301 | freq: Frequency | None = None, |
| 16302 | axis: Axis = 0, |
| 16303 | copy: bool | lib.NoDefault = lib.no_default, |
| 16304 | ) -> DataFrame: |
| 16305 | """ |
| 16306 | Convert DataFrame from DatetimeIndex to PeriodIndex. |
| 16307 | |
| 16308 | Convert DataFrame from DatetimeIndex to PeriodIndex with desired |
| 16309 | frequency (inferred from index if not passed). Either index of columns can be |
| 16310 | converted, depending on `axis` argument. |
| 16311 | |
| 16312 | Parameters |
| 16313 | ---------- |
| 16314 | freq : str, default |
| 16315 | Frequency of the PeriodIndex. |
| 16316 | axis : {0 or 'index', 1 or 'columns'}, default 0 |
| 16317 | The axis to convert (the index by default). |
| 16318 | copy : bool, default False |
| 16319 | This keyword is now ignored; changing its value will have no |
| 16320 | impact on the method. |
| 16321 | |
| 16322 | .. deprecated:: 3.0.0 |
| 16323 | |
| 16324 | This keyword is ignored and will be removed in pandas 4.0. Since |
| 16325 | pandas 3.0, this method always returns a new object using a lazy |
| 16326 | copy mechanism that defers copies until necessary |
| 16327 | (Copy-on-Write). See the `user guide on Copy-on-Write |
| 16328 | <https://pandas.pydata.org/docs/dev/user_guide/copy_on_write.html>`__ |
| 16329 | for more details. |
| 16330 | |
| 16331 | Returns |
| 16332 | ------- |
| 16333 | DataFrame |
| 16334 | The DataFrame with the converted PeriodIndex. |
| 16335 | |
| 16336 | See Also |
| 16337 | -------- |
| 16338 | Series.to_period: Equivalent method for Series. |
| 16339 | Series.dt.to_period: Convert DateTime column values. |
| 16340 | |
| 16341 | Examples |
| 16342 | -------- |
| 16343 | >>> idx = pd.to_datetime( |
| 16344 | ... [ |
| 16345 | ... "2001-03-31 00:00:00", |
| 16346 | ... "2002-05-31 00:00:00", |
| 16347 | ... "2003-08-31 00:00:00", |
| 16348 | ... ] |
| 16349 | ... ) |
| 16350 | |
| 16351 | >>> idx |
| 16352 | DatetimeIndex(['2001-03-31', '2002-05-31', '2003-08-31'], |
| 16353 | dtype='datetime64[us]', freq=None) |
| 16354 | |
| 16355 | >>> idx.to_period("M") |
| 16356 | PeriodIndex(['2001-03', '2002-05', '2003-08'], dtype='period[M]') |