pandas 1.4.2

NotesParametersRaisesReturns
round(self, freq, ambiguous='raise', nonexistent='raise')

Notes

If the timestamps have a timezone, rounding will take place relative to the local ("wall") time and re-localized to the same timezone. When rounding near daylight savings time, use nonexistent and ambiguous to control the re-localization behavior.

Parameters

freq : str or Offset

The frequency level to round the index to. Must be a fixed frequency like 'S' (second) not 'ME' (month end). See frequency aliases <timeseries.offset_aliases> for a list of possible :None:None:`freq` values.

ambiguous : 'infer', bool-ndarray, 'NaT', default 'raise'

Only relevant for DatetimeIndex:

  • 'infer' will attempt to infer fall dst-transition hours based on order

  • bool-ndarray where True signifies a DST time, False designates a non-DST time (note that this flag is only applicable for ambiguous times)

  • 'NaT' will return NaT where there are ambiguous times

  • 'raise' will raise an AmbiguousTimeError if there are ambiguous times.

nonexistent : 'shift_forward', 'shift_backward', 'NaT', timedelta, default 'raise'

A nonexistent time does not exist in a particular timezone where clocks moved forward due to DST.

  • 'shift_forward' will shift the nonexistent time forward to the closest existing time

  • 'shift_backward' will shift the nonexistent time backward to the closest existing time

  • 'NaT' will return NaT where there are nonexistent times

  • timedelta objects will shift nonexistent times by the timedelta

  • 'raise' will raise an NonExistentTimeError if there are nonexistent times.

Raises

ValueError if the `freq` cannot be converted.

Returns

DatetimeIndex, TimedeltaIndex, or Series

Index of the same type for a DatetimeIndex or TimedeltaIndex, or a Series with the same index for a Series.

Perform round operation on the data to the specified :None:None:`freq`.

Examples

DatetimeIndex

This example is valid syntax, but we were not able to check execution
>>> rng = pd.date_range('1/1/2018 11:59:00', periods=3, freq='min')
... rng DatetimeIndex(['2018-01-01 11:59:00', '2018-01-01 12:00:00', '2018-01-01 12:01:00'], dtype='datetime64[ns]', freq='T')
This example is valid syntax, but we were not able to check execution
>>> rng.round('H')
DatetimeIndex(['2018-01-01 12:00:00', '2018-01-01 12:00:00',
               '2018-01-01 12:00:00'],
              dtype='datetime64[ns]', freq=None)

Series

This example is valid syntax, but we were not able to check execution
>>> pd.Series(rng).dt.round("H")
0   2018-01-01 12:00:00
1   2018-01-01 12:00:00
2   2018-01-01 12:00:00
dtype: datetime64[ns]

When rounding near a daylight savings time transition, use ambiguous or nonexistent to control how the timestamp should be re-localized.

This example is valid syntax, but we were not able to check execution
>>> rng_tz = pd.DatetimeIndex(["2021-10-31 03:30:00"], tz="Europe/Amsterdam")
This example is valid syntax, but we were not able to check execution
>>> rng_tz.floor("2H", ambiguous=False)
DatetimeIndex(['2021-10-31 02:00:00+01:00'],
              dtype='datetime64[ns, Europe/Amsterdam]', freq=None)
This example is valid syntax, but we were not able to check execution
>>> rng_tz.floor("2H", ambiguous=True)
DatetimeIndex(['2021-10-31 02:00:00+02:00'],
              dtype='datetime64[ns, Europe/Amsterdam]', freq=None)
See :

Local connectivity graph

Hover to see nodes names; edges to Self not shown, Caped at 50 nodes.

Using a canvas is more power efficient and can get hundred of nodes ; but does not allow hyperlinks; , arrows or text (beyond on hover)

SVG is more flexible but power hungry; and does not scale well to 50 + nodes.

All aboves nodes referred to, (or are referred from) current nodes; Edges from Self to other have been omitted (or all nodes would be connected to the central node "self" which is not useful). Nodes are colored by the library they belong to, and scaled with the number of references pointing them


File: /pandas/core/arrays/datetimelike.py#1773
type: <class 'function'>
Commit: