From the below data, i need to create daywise chunk through iteration
data:
lable datetime value daytime
0 tag1 5/30/2022 0:00 361.378 2022-05-30 00:00:00
1 tag1 5/30/2022 0:01 344.072 2022-05-30 00:01:00
2 tag1 5/30/2022 0:02 321.001 2022-05-30 00:02:00
3 tag1 5/30/2022 0:03 296.715 2022-05-30 00:03:00
4 tag1 5/30/2022 0:04 272.290 2022-05-30 00:04:00
5 tag1 5/30/2022 0:05 250.708 2022-05-30 00:05:00
6 tag1 5/31/2022 0:06 283.087 2022-05-31 00:06:00
7 tag1 5/31/2022 0:07 314.322 2022-05-31 00:07:00
8 tag1 5/31/2022 0:08 345.811 2022-05-31 00:08:00
9 tag1 5/31/2022 0:09 362.085 2022-05-31 00:09:00
10 tag1 5/31/2022 0:10 363.021 2022-05-31 00:10:00
11 tag1 5/31/2022 0:11 364.575 2022-05-31 00:11:00
12 tag1 5/31/2022 0:12 361.114 2022-05-31 00:12:00
I have used the below code. I am creating a "daytime" column from datetime as i was not able to use the groupby. it was giving type error TypeError: Only valid with DatetimeIndex, TimedeltaIndex or PeriodIndex, but got an instance of 'Index'
(when i was trying data.groupby(pd.Grouper(key='datetime', freq='D'))
data['daytime'] = pd.to_datetime(data.datetime)
data_grp = data.groupby(pd.Grouper(key='daytime', freq='D'))
then to get the daywise chunk, i use the below code
for i in data_grp:
day_chunk = i
print type(day_chunk)
Output:
(Timestamp('2022-05-30 00:00:00', freq='D'), lable datetime value daytime
0 tag1 5/30/2022 0:00 361.378 2022-05-30 00:00:00
1 tag1 5/30/2022 0:01 344.072 2022-05-30 00:01:00
2 tag1 5/30/2022 0:02 321.001 2022-05-30 00:02:00
3 tag1 5/30/2022 0:03 296.715 2022-05-30 00:03:00
4 tag1 5/30/2022 0:04 272.290 2022-05-30 00:04:00
5 tag1 5/30/2022 0:05 250.708 2022-05-30 00:05:00)
<type 'tuple'>
(Timestamp('2022-05-31 00:00:00', freq='D'), lable datetime value daytime
6 tag1 5/31/2022 0:06 283.087 2022-05-31 00:06:00
7 tag1 5/31/2022 0:07 314.322 2022-05-31 00:07:00
8 tag1 5/31/2022 0:08 345.811 2022-05-31 00:08:00
9 tag1 5/31/2022 0:09 362.085 2022-05-31 00:09:00
10 tag1 5/31/2022 0:10 363.021 2022-05-31 00:10:00
11 tag1 5/31/2022 0:11 364.575 2022-05-31 00:11:00
12 tag1 5/31/2022 0:12 361.114 2022-05-31 00:12:00)
<type 'tuple'>
I want output as a dataframe and not tuple. can you please tell me how to get the day_chuck as pandas.core.frame.DataFrame
CodePudding user response:
You can use
for name, group in data_grp:
day_chunk = group