w3resource

Pandas Series: dt.round() function

Series.dt.round() function

The dt.round() function performs round operation on the data to the specified frequency.

Syntax:

Series.dt.round(self, *args, **kwargs)
Pandas Series: dt.round() function

Parameter:

Name Description Type / Default Value Required / Optional
freq The frequency level to round the index to. Must be a fixed frequency like 'S' (second) not 'ME' (month end). str or Offset Required
ambiguous 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
'infer', bool-ndarray, 'NaT', default 'raise' Required
nonexistent 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
'shift_forward', 'shift_backward', 'NaT', timedelta, default 'raise' Required

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.

Raises: ValueError if the freq cannot be converted.

Example - DatetimeIndex:

Python-Pandas Code:

import numpy as np
import pandas as pd
rng = pd.date_range('1/2/2019 11:59:00', periods=4, freq='min')
rng

Output:

DatetimeIndex(['2019-01-02 11:59:00', '2019-01-02 12:00:00',
               '2019-01-02 12:01:00', '2019-01-02 12:02:00'],
              dtype='datetime64[ns]', freq='T')

Python-Pandas Code:

import numpy as np
import pandas as pd
rng = pd.date_range('1/2/2019 11:59:00', periods=4, freq='min')
rng.round('H')

Output:

DatetimeIndex(['2019-01-02 12:00:00', '2019-01-02 12:00:00',
               '2019-01-02 12:00:00', '2019-01-02 12:00:00'],
              dtype='datetime64[ns]', freq=None)
Pandas Series: dt.round() function

Example - Series:

Python-Pandas Code:

import numpy as np
import pandas as pd
rng = pd.date_range('1/2/2019 11:59:00', periods=4, freq='min')
pd.Series(rng).dt.round("H")

Output:

0   2019-01-02 12:00:00
1   2019-01-02 12:00:00
2   2019-01-02 12:00:00
3   2019-01-02 12:00:00
dtype: datetime64[ns]

Previous: Series.dt.strftime() function
Next: Series.dt.floor() function



Become a Patron!

Follow us on Facebook and Twitter for latest update.

It will be nice if you may share this link in any developer community or anywhere else, from where other developers may find this content. Thanks.

https://w3resource.com/pandas/series/series-dt-round.php