How to round date time index in a pandas data frame?

pandas datetime round
pandas compare datetime
pandas round index
pandas index datetime
pandas dt
time series pandas
pandas add datetime column
pandas datetime arithmetic

There is a pandas dataframe like this:

index
2018-06-01 02:50:00     R 45.48 -2.8 
2018-06-01 07:13:00     R 45.85 -2.0  
... 
2018-06-01 08:37:00     R 45.87  -2.7

I would like to round the index to the hour like this:

index
2018-06-01 02:00:00     R 45.48 -2.8 
2018-06-01 07:00:00     R 45.85 -2.0  
... 
2018-06-01 08:00:00     R 45.87  -2.7

I am trying the following code:

df = df.date_time.apply ( lambda x : x.round('H'))

but returns a serie instead of a dataframe with the modified index column

Try using floor:

df.index.floor('H')

Setup:

df = pd.DataFrame(np.arange(25),index=pd.date_range('2018-01-01 01:12:50','2018-01-02 01:12:50',freq='H'),columns=['Value'])
df.head()
                    Value
2018-01-01 01:12:50 0
2018-01-01 02:12:50 1
2018-01-01 03:12:50 2
2018-01-01 04:12:50 3
2018-01-01 05:12:50 4

df.index = df.index.floor('H')
df.head()
                    Value
2018-01-01 01:00:00 0
2018-01-01 02:00:00 1
2018-01-01 03:00:00 2
2018-01-01 04:00:00 3
2018-01-01 05:00:00 4

pandas.DatetimeIndex.round, 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. pandas.DataFrame.round¶ DataFrame.round (self, decimals=0, *args, **kwargs) → 'DataFrame' [source] ¶ Round a DataFrame to a variable number of decimal places. Parameters decimals int, dict, Series. Number of decimal places to round each column to. If an int is given, round each column to the same number of places.

Try this

df['index'].apply(lambda dt: datetime.datetime(dt.year, dt.month, dt.day, dt.hour,60*(dt.minute // 60)))

How to round a Pandas `DatetimeIndex`?, Update: if you're doing this to a DatetimeIndex / datetime64 column a better way is to use np.round directly rather than via an apply/map: New in version 0.24.0. Set the index of the returned DataFrame as the original Index. The passed name should substitute for the index name (if it has one). DataFrame containing the original Index data. Convert an Index to a Series. Convert Series to DataFrame. By default, the original Index is reused.

Try my method:

Add a new column by the rounded value of hour:

df['E'] = df.index.round('H')

Set it as index:

df1 = df.set_index('E')

Delete the name you set('E' here):

df1.index.name = None

And now, df1 is a new DataFrame with index hour rounded from df.

Python, Pandas DatetimeIndex.round() function localize tz-naive DatetimeIndex to type for a DatetimeIndex or TimedeltaIndex, or a Series with the same index for a  freq : string or pandas offset object, optional. One of pandas date offset strings or corresponding objects. The string ‘infer’ can be passed in order to set the frequency of the index as the inferred frequency upon creation.

Python, Returns : DatetimeIndex, TimedeltaIndex, or Series. Example #1: Use Series.dt.​round() function to round the datetime data of the given series object to the  Set the DataFrame index using existing columns. Set the DataFrame index (row labels) using one or more existing columns or arrays (of the correct length). The index can replace the existing index or expand on it.

pandas.Series.dt.round, Navigation. index · modules |; next |; previous |; pandas 0.22.0 documentation »; API Reference ». Table Of Contents. What's New · Installation · Contributing to  Pandas is one of those packages and makes importing and analyzing data much easier. Pandas dataframe.round() function is used to round a DataFrame to a variable number of decimal places. This function provides the flexibility to round different columns by different places. Syntax:DataFrame.round(decimals=0, *args, **kwargs) Parameters :

ENH: add rounding method to DatetimeIndex/TimedeltaIndex · Issue , http://stackoverflow.com/questions/17781159/round-pandas-datetime-index About the .dt : if we can change Series.round to handle datetime64 values, I don't​  Convert argument to datetime. Specify a date parse order if arg is str or its list-likes. If True, parses dates with the day first, eg 10/11/12 is parsed as 2012-11-10. Warning: dayfirst=True is not strict, but will prefer to parse with day first (this is a known bug, based on dateutil behavior). Specify a date parse order if arg is str or its