Pandas Series: dt.floor() function
Series.dt.floor() function
The dt.floor() function performs floor operation on the data to the specified freq.
Syntax:
Series.dt.floor(self, *args, **kwargs)
Parameter:
Name | Description | Type / Default Value | Required / Optional |
---|---|---|---|
freq | The frequency level to floor 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', 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', '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.floor('H')
Output:
DatetimeIndex(['2019-01-02 11: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)
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.floor("H")
Output:
0 2019-01-02 11: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.round() function
Next: Series.dt.ceil() function
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-floor.php
- Weekly Trends and Language Statistics
- Weekly Trends and Language Statistics