How to access dictionary when index is datetime?

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP


How to access dictionary when index is datetime?



I have multiple csv-files each containing two columns. The first contains the timestamp and the second column contains a measurement value.



Lets assume the csv-file looks like this:


Date,Temperature (Celsius)
2018-07-24T20:45:31,28.86
2018-07-24T20:35:31,29.06
2018-07-24T20:25:31,29.19
2018-07-24T20:15:32,29.31
2018-07-24T20:05:31,29.48
2018-07-24T19:55:31,29.58
2018-07-24T19:45:31,29.82
2018-07-24T19:35:32,30.32
2018-07-24T19:25:31,31.00



and I import it like this:


df = pd.read_csv(csv-file, sep=',', header = 0, usecols=[0, 1], parse_dates=[0], infer_datetime_format=True)
dd = df.set_index('Date').T.to_dict()



I thought this would be convenient later, when I would search for the same key (same timestamp) in multiple files in order to merge them.
However I cannot access the dictionary. The keys are recognised as timestamp.


dd.keys()

dict_keys([Timestamp('2018-07-24 20:45:31'), Timestamp('2018-07-24 20:35:31'), Timestamp('2018-07-24 20:25:31'), Timestamp('2018-07-24 20:15:32'), Timestamp('2018-07-24 20:05:31'), Timestamp('2018-07-24 19:55:31'), Timestamp('2018-07-24 19:45:31'), Timestamp('2018-07-24 19:35:32'), Timestamp('2018-07-24 19:25:31')])



How do I access the values?


dd["2018-07-24 20:45:31"]["Temperature (Celsius)"]



does not work, so I tried


dd[Timestamp("2018-07-24 20:45:31")]["Temperature (Celsius)"]



which does not work either.



I was wondering if it might be forbidden to use a timestamp as a key, but if not, than how do I access my data?





I guess your problem is already solved. Check this link out.
– AntiConformiste.97
13 mins ago





I found this question as well, but I could not figure out how I access the data. Could you help me out here? How do I read out the temperature at this (2018-07-24 20:45:31) point in time?
– rul30
8 mins ago




1 Answer
1



Each of the Timestamp keys you see are instances of pandas.Timestamp. You should pass your keys with pd.Timestamp, like so:


Timestamp


pandas.Timestamp


pd.Timestamp


import pandas as pd

df = pd.read_csv(
'file.csv', sep=',', header = 0, usecols=[0, 1], parse_dates=[0], infer_datetime_format=True
)
dd = df.set_index('Date').T.to_dict()

print(dd[pd.Timestamp('2018-07-24 20:45:31')]['Temperature (Celsius)']) # prints 28.86





👍 thanks, I did check whether the conversion preserves it, but I did not think about using it when looking through a dictionary. SOLVED.
– rul30
4 mins ago





Alternatively, you can add a from pandas import Timestamp and your code will work.
– Tomas Farias
3 mins ago


from pandas import Timestamp






By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Popular posts from this blog

Visual Studio Code: How to configure includePath for better IntelliSense results

Spring cloud config client Could not locate PropertySource

Regex - How to capture all iterations of a repeating pattern?