将熊猫的日期时间转换为小时:分钟四舍五入到15分钟

我使用以下命令使用熊猫阅读此excell工作表(仅“ DATEHEUREMAX”列):

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

将熊猫的日期时间转换为小时:分钟四舍五入到15分钟

现在我想将此df转换为简化的df,仅将hour:min向上舍入为15min。主要思想是根据小时:分钟绘制直方图

qwq200481 回答:将熊猫的日期时间转换为小时:分钟四舍五入到15分钟

我想这就是你要的

    .navigationBarItems(leading: VStack {
        HStack(spacing: 100) {
            Text("Find People").font(.system(size: 30)).bold()
            Text("Follow All").foregroundColor(Color(ColorUtils.hexStringToUIColor(hex: Constants.THEME.THEME_COLOR)))
        }
        HStack(spacing: 100) {
            Text("Import from: ")
            ForEach(socialIcons,id: \.self) {icon in
                Image(icon).resizable().frame(width: 25,height: 25)
            }
        }
    }) /* `trailing: nil` removed */

尽管我同意评论者的意见,您可能并不需要这样做,而只是使用时间分组器

,

考虑以下具有单个列的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()

此代码解析为如下图: enter image description here

数据解决方案

对于您的数据,您应该可以执行以下操作:

import pandas as pd
import matplotlib.pyplot as plt
xdata = read_excel('Data.xlsx','Data',usecols=['DATEHEUREMAX'])
xdata.set_index('DATEHEUREMAX',inplace=True)
xdata.groupby(pd.Grouper(freq='15Min')).count().plot(kind='bar')
plt.show()
本文链接:https://www.f2er.com/3018516.html

大家都在问