The following code takes a dict of dates and values and convert it to df, i have the following dict (shortened):
{'2022-09-18 06:00:00': 5.4,
'2022-09-18 07:00:00': 6.0,
'2022-09-18 08:00:00': 6.5,
'2022-09-18 14:00:00': 7.9,
'2022-09-18 15:00:00': 7.8,
'2022-09-18 16:00:00': 7.6,
'2022-09-18 17:00:00': 6.8,
'2022-09-18 18:00:00': 6.4,
'2022-09-18 19:00:00': 5.7,
'2022-09-18 20:00:00': 4.8,
'2022-09-18 21:00:00': 5.4,
'2022-09-18 22:00:00': 4.7,
'2022-09-18 23:00:00': 4.3,
'2022-09-19 04:00:00': 3.5,
'2022-09-19 05:00:00': 2.8,
'2022-09-19 06:00:00': 3.8}
And the following cod:
def dict_to_df(date_dict):
df = pd.DataFrame(list(date_value.items()), columns=['DateTime', 'Value'])
df['DateTime'] = pd.to_datetime(df.DateTime, format='%Y-%m-%d %H:%M:%S')
df['Date'] = df['DateTime'].dt.strftime('%d/%m/%Y')
df['Time'] = df['DateTime'].dt.strftime('%H:%M')
df['period'] = pd.cut(pd.to_datetime(df.DateTime).dt.hour,
bins=[0, 5, 17, 23],
labels=['night', 'morning', 'night'],
include_lowest=True, ordered= False)
df['DatePeriod']=df['Date'].astype(str) '-' df['period'].astype(str)
result_df = (df.groupby('DatePeriod')
.agg({"Value": list, ("DateTime"): "first"})
.set_index("DateTime")["Value"]
.to_dict())
return result_df
But when i try to convert my dict i get
{Timestamp('2022-09-18 06:00:00'): [5.4, 6.0, 6.5, 6.7, 6.9, 7.9, 8.5, 7.5, 7.9, 7.8, 6.8],
Timestamp('2022-09-18 18:00:00'): [6.4, 5.7, 4.8, 5.4, 4.7, 4.3],
Timestamp('2022-09-19 06:00:00'): [3.8],
Timestamp('2022-09-19 00:00:00'): [4.1, 4.4, 4.3, 3.8, 3.5, 2.8]}
I want the dates to be the same, It's really confusing that it's saying that the dates are in Timestamp? What's the problem here? Thanks i advance!
CodePudding user response:
Date is still the same, its an instance of Timestamp
class from Pandas . There's no any problem at all. But you can convert the timestamp value to string if you want datetimes to be represented as string instead of Timestamp object:
(df.assign(sdatetime=df['DateTime'].astype(str)).groupby('DatePeriod')
.agg({"Value": list, ("sdatetime"): "first"})
.set_index("sdatetime")["Value"]
.to_dict())
{'2022-09-18 06:00:00': [5.4, 6.0, 6.5, 7.9, 7.8, 7.6, 6.8],
'2022-09-18 18:00:00': [6.4, 5.7, 4.8, 5.4, 4.7, 4.3],
'2022-09-19 06:00:00': [3.8],
'2022-09-19 04:00:00': [3.5, 2.8]
}