1

I read this excell sheet (only column of 'DATEHEUREMAX') with pandas using this command:

xdata = read_excel('Data.xlsx', 'Data', usecols=['DATEHEUREMAX'])

exemple of my excel sheet

now I want to turn this df into a simplify df with only hour:min rounded to 15min up. The main idea is to plot an histogram base on hour:min

Jonathan Roy
  • 405
  • 1
  • 6
  • 18

3 Answers3

0

I think this is what you are asking for

rounded_column = df['time_column'].dt.round('15min').strftime("%H:%M")

although i agree with the commenters you might not really need to do this and just use a timegrouper

Joran Beasley
  • 110,522
  • 12
  • 160
  • 179
0

Consider the following DataFrame, with a single column, read as datetime (not string):

                  Dat
0 2019-06-03 12:07:00
1 2019-06-04 10:04:00
2 2019-06-05 11:42:00
3 2019-06-06 10:17:00

To round these dates to 15 mins run:

df['Dat2'] = df.Dat.dt.round('15T').dt.time.map(lambda s: str(s)[:-3])

The result is:

                  Dat  Dat2
0 2019-06-03 12:07:00 12:00
1 2019-06-04 10:04:00 10:00
2 2019-06-05 11:42:00 11:45
3 2019-06-06 10:17:00 10:15

For demonstration purpose, I saved the result in a new column, but you can save it in the original column.

Valdi_Bo
  • 30,023
  • 4
  • 23
  • 41
0

There is no need to round your column in order to get a histogram of dates with your DATEHEUREMAX column. For this purpose you can just make use of pd.Grouper as detailed below.

Toy sample code

You can work out this example to get a solution with your date column:

import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
# Generating a sample of 10000 timestamps and selecting 500 to randomize them
df = pd.DataFrame(np.random.choice(pd.date_range(start=pd.to_datetime('2015-01-14'),periods = 10000, freq='S'), 500),  columns=['date'])
# Setting the date as the index since the TimeGrouper works on Index, the date column is not dropped to be able to count
df.set_index('date', drop=False, inplace=True)
# Getting the histogram
df.groupby(pd.Grouper(freq='15Min')).count().plot(kind='bar')
plt.show()

This code resolves to a graph like below: enter image description here

Solution with your data

For your data you should be able to do something like:

import pandas as pd
import matplotlib.pyplot as plt
xdata = read_excel('Data.xlsx', 'Data', usecols=['DATEHEUREMAX'])
xdata.set_index('DATEHEUREMAX', drop=False, inplace=True)
xdata.groupby(pd.Grouper(freq='15Min')).count().plot(kind='bar')
plt.show()
Cedric Zoppolo
  • 4,271
  • 6
  • 29
  • 59