Reputation: 194
I have a dictionary containing results of a user's social media activity, the values of the dictionary are in a list containing Timestamp objects and I want to convert it to time objects (datetime.time())
here is the dictionary
{{'Instagram':[Timestamp('2020-08-23 04:16:05.12456'), Timestamp('2020-08-23 04:17:02.88754'), Timestamp('2020-08-23 05:20:21.43215'), Timestamp('2020-08-23 06:21:19.63441'), Timestamp('2020-08-23 08:23:15.76421')]},
{'Twitter':[Timestamp('2020-08-23 05:19:12.21245'), Timestamp('2020-08-23 06:21:10.09875'), TiTimestamp('2020-08-23 07:22:08.65784'),Timestamp('2020-08-23 08:23:25.09123')]},
{'Facebook':[Timestamp('2020-08-23 04:1:46.436778'), Timestamp('2020-08-23 05:19:19.34213'), Timestamp('2020-08-23 05:20:25.56784'), Timestamp('2020-08-23 08:23:12.22567')]}}
and here is the code I wrote to convert the Timestamp object to datetime object
for i in range(2, len(dataset.columns)):
(d[dataset.columns[i]])=pd.to_datetime(d[dataset.columns[i]], unit = "ms")
But I get an error stating: ValueError: unit='ms' not valid with non-numerical val='2020-08-23 04:16:05.12456'
Upvotes: 1
Views: 277
Reputation: 862671
Use neste dict with list comprehension:
d = {k: [x.time() for x in v] for k, v in d.items()}
But if processing solution from this you can create new column filled by times and pass after groupby()
:
df['d'] = pd.to_datetime(df['DateTime'], format='(%Y,%m,%d,%H,%M,%S)')
#added time column
df['time'] = df['d'].dt.time
day1 = df['d'].dt.date[0]
df = df[df['d'].dt.date.eq(day1)]
df = df.melt(['DateTime','d'])
df = df[df['value'].eq('Y')]
d = df.groupby('variable')['time'].agg(list).to_dict()
Upvotes: 1