将 pandas 日期时间转为 hour:min 四舍五入为 15 分钟
Turn pandas datetime to hour:min rounded to 15 min
我用 pandas 使用此命令读取了此 excell sheet(仅 'DATEHEUREMAX' 的列):
xdata = read_excel('Data.xlsx', 'Data', usecols=['DATEHEUREMAX'])
现在我想把这个 df 变成一个简化的 df,只有 hour:min 四舍五入到 15 分钟。主要思想是根据 hour:min
绘制直方图
我想这就是你想要的
rounded_column = df['time_column'].dt.round('15min').strftime("%H:%M")
虽然我同意评论者的意见,但您可能真的不需要这样做,只需使用时间分组器
考虑以下 DataFrame,单列,读作 datetime(不是 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
将这些日期四舍五入到 15 分钟 运行:
df['Dat2'] = df.Dat.dt.round('15T').dt.time.map(lambda s: str(s)[:-3])
结果是:
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
出于演示目的,我将结果保存在新列中,但您可以
将其保存在原始列中。
无需四舍五入即可获得 DATEHEUREMAX
列的日期直方图。为此,您可以使用 pd.Grouper
,详情如下。
玩具示例代码
您可以使用此示例来获得日期列的解决方案:
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()
此代码解析为如下图:
用你的数据解决
对于您的数据,您应该能够执行以下操作:
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()
我用 pandas 使用此命令读取了此 excell sheet(仅 'DATEHEUREMAX' 的列):
xdata = read_excel('Data.xlsx', 'Data', usecols=['DATEHEUREMAX'])
现在我想把这个 df 变成一个简化的 df,只有 hour:min 四舍五入到 15 分钟。主要思想是根据 hour:min
绘制直方图我想这就是你想要的
rounded_column = df['time_column'].dt.round('15min').strftime("%H:%M")
虽然我同意评论者的意见,但您可能真的不需要这样做,只需使用时间分组器
考虑以下 DataFrame,单列,读作 datetime(不是 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
将这些日期四舍五入到 15 分钟 运行:
df['Dat2'] = df.Dat.dt.round('15T').dt.time.map(lambda s: str(s)[:-3])
结果是:
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
出于演示目的,我将结果保存在新列中,但您可以 将其保存在原始列中。
无需四舍五入即可获得 DATEHEUREMAX
列的日期直方图。为此,您可以使用 pd.Grouper
,详情如下。
玩具示例代码
您可以使用此示例来获得日期列的解决方案:
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()
此代码解析为如下图:
用你的数据解决
对于您的数据,您应该能够执行以下操作:
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()