Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/288.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 从一天开始按n天对数据帧进行分组_Python_Pandas - Fatal编程技术网

Python 从一天开始按n天对数据帧进行分组

Python 从一天开始按n天对数据帧进行分组,python,pandas,Python,Pandas,我刚刚发现了熊猫的力量,我喜欢它,但我无法解决这个问题: 我有一个数据帧df.head(): 我想把我的数据分成九天的间隔 gb = df.groupby(pd.TimeGrouper(key='time', freq='9D')) 第一组: 2002-05-15 12:59:31.717467 lon lat h filename time 0 19.961216 80.617627 -0.077165 60048 2

我刚刚发现了熊猫的力量,我喜欢它,但我无法解决这个问题:

我有一个数据帧
df.head()

我想把我的数据分成九天的间隔

gb = df.groupby(pd.TimeGrouper(key='time', freq='9D'))
第一组:

2002-05-15 12:59:31.717467       lon   lat  h filename                  time
0    19.961216  80.617627    -0.077165     60048 2002-05-15 12:59:31.717467
1    19.923916  80.614847    -0.018689     60048 2002-05-15 12:59:31.831467
2    19.849396  80.609257    -0.089205     60048 2002-05-15 12:59:32.059467
3    19.830776  80.607857     0.076485     60048 2002-05-15 12:59:32.116467
...
下一组:

2002-05-24 12:59:31.717467        lon   lat  height  filename                  time
815   18.309498  80.457024     0.187387     60309 2002-05-24 16:35:39.553563
816   18.291458  80.458514     0.061446     60309 2002-05-24 16:35:39.610563
817   18.273408  80.460014     0.129255     60309 2002-05-24 16:35:39.667563
818   18.255358  80.461504     0.046761     60309 2002-05-24 16:35:39.724563
...
因此,数据从第一次开始(12:59:31.717467)分为九天进行分组,而不是像我希望的那样从一天开始

按一天分组时:

gb = df.groupby(pd.TimeGrouper(key='time', freq='D'))
给我:

2002-05-15 00:00:00       lon   lat  h  filename                  time
0    19.961216  80.617627    -0.077165     60048 2002-05-15 12:59:31.717467
1    19.923916  80.614847    -0.018689     60048 2002-05-15 12:59:31.831467
2    19.849396  80.609257    -0.089205     60048 2002-05-15 12:59:32.059467
3    19.830776  80.607857     0.076485     60048 2002-05-15 12:59:32.116467
...
我可以循环几天,直到我得到一个九天的间隔,但我认为这可以做得更聪明,我正在寻找一个石斑鱼
freq
选项,相当于YS(年初)的天数,一种设置开始时间的方式(可能通过石斑鱼选项
约定:{'start',end',e',s'}
),或者


我正在运行Python 3.5.2,Pandas的版本为:0.19.0

如果将日期时间截断到给定日期的午夜,分组将按预期工作(从一天开始)。我希望它通过转换为datetimes来工作,例如

df['date'] = df['time'].apply(lambda x:x.date())
但是,除非索引是
datetime
,否则不能使用
TimeGrouper
。 相反,您有两个选项,或者直接将日期时间截断为午夜,如下所示:

df['date'] = df['time'].apply(lambda x:x.replace(hour=0, minute=0, second=0, microsecond=0)))
或者,您可以先生成
date
值,然后使用
pd.to\u datetime()
函数将其转换回日期时间:

df['date'] = df['time'].apply(lambda x: x.date() )
df['date'] = pd.to_datetime(df['date'])

完成@mfitzp answer您可以这样做:

df['dateonly'] = df['time'].apply(lambda x: x.date())
唯一的问题是
df['dateonly']
不会是DatetimeIndex

您需要先将其转换为:

df['dateonly'] = pd.to_datetime(df['dateonly'])
现在你可以分组了

gb = df.groupby(pd.TimeGrouper(key='dateonly', freq='9D'))
对于额外信息,
约定
周期索引
一起使用,而不是
日期时间索引

删除第一时间行:

您最好选择
datetime
列的第一行,以便将时间重置为
00:00:00
(午夜),并根据9D间隔分组:

df.loc[0, 'time'] = df['time'].iloc[0].normalize()
for _, grp in df.groupby(pd.TimeGrouper(key='time', freq='9D')):
    print (grp)

#          lon        lat         h  filename                       time
# 0  19.961216  80.617627 -0.077165     60048 2002-05-15 00:00:00.000000
# 1  19.923916  80.614847 -0.018689     60048 2002-05-15 12:59:31.831467
# 2  19.849396  80.609257 -0.089205     60048 2002-05-15 12:59:32.059467
# 3  19.830776  80.607857  0.076485     60048 2002-05-15 12:59:32.116467
# 4  19.570708  80.588183  0.162943     60048 2002-05-15 12:59:32.888467
# ......................................................................
这将恢复其他行中的时间,因此不会丢失这些信息


保留第一时间行:

如果希望保持第一时间行的原样,而不对其进行任何更改,但只希望从午夜开始分组,则可以执行以下操作:

df_t_shift = df.shift()    # Shift one level down
df_t_shift.loc[0, 'time'] = df_t_shift['time'].iloc[1].normalize()
# Concat last row of df with the shifted one to account for the loss of row
df_t_shift = df_t_shift.append(df.iloc[-1], ignore_index=True)  

for _, grp in df_t_shift.groupby(pd.TimeGrouper(key='time', freq='9D')):
    print (grp)

#          lon        lat         h  filename                       time
# 0        NaN        NaN       NaN       NaN 2002-05-15 00:00:00.000000
# 1  19.961216  80.617627 -0.077165   60048.0 2002-05-15 12:59:31.717467
# 2  19.923916  80.614847 -0.018689   60048.0 2002-05-15 12:59:31.831467
# 3  19.849396  80.609257 -0.089205   60048.0 2002-05-15 12:59:32.059467
# 4  19.830776  80.607857  0.076485   60048.0 2002-05-15 12:59:32.116467
# 5  19.570708  80.588183  0.162943   60048.0 2002-05-15 12:59:32.888467

建议添加参数
closed='left'
可以吗?我已经尝试过了,但没有改变任何东西
convention='s'
可以做什么吗?文档中严重缺乏关于
TimeGrouper
参数的功能。如果无法使其正常工作,另一个选项是将列转换为日期(而不是日期时间),这将删除时间组件(那天的午夜四舍五入)。是,我找不到使用
约定的示例。我刚刚试过使用
convention='s'
,但没有成功。谢谢你的回答谢谢你的回答谢谢你的回答
df_t_shift = df.shift()    # Shift one level down
df_t_shift.loc[0, 'time'] = df_t_shift['time'].iloc[1].normalize()
# Concat last row of df with the shifted one to account for the loss of row
df_t_shift = df_t_shift.append(df.iloc[-1], ignore_index=True)  

for _, grp in df_t_shift.groupby(pd.TimeGrouper(key='time', freq='9D')):
    print (grp)

#          lon        lat         h  filename                       time
# 0        NaN        NaN       NaN       NaN 2002-05-15 00:00:00.000000
# 1  19.961216  80.617627 -0.077165   60048.0 2002-05-15 12:59:31.717467
# 2  19.923916  80.614847 -0.018689   60048.0 2002-05-15 12:59:31.831467
# 3  19.849396  80.609257 -0.089205   60048.0 2002-05-15 12:59:32.059467
# 4  19.830776  80.607857  0.076485   60048.0 2002-05-15 12:59:32.116467
# 5  19.570708  80.588183  0.162943   60048.0 2002-05-15 12:59:32.888467