0

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?

rul30
  • 463
  • 1
  • 7
  • 17
  • I guess your problem is already solved. Check this [link](https://stackoverflow.com/questions/35946387/python-dictionary-datetime-as-key-keyerror) out. – BELGOUGI Jul 29 '18 at 15:13
  • 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 Jul 29 '18 at 15:18

1 Answers1

1

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

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
Tomas Farias
  • 1,293
  • 1
  • 14
  • 18
  • thanks, I did check whether the conversion preserves it, but I did not think about using it when looking through a dictionary. SOLVED. – rul30 Jul 29 '18 at 15:22
  • 1
    Alternatively, you can add a `from pandas import Timestamp` and your code will work. – Tomas Farias Jul 29 '18 at 15:23
  • 1
    I see, I was under the impression that the "Timestamp" was somehow universal not tied to pandas. Thanks for pointing that out. – rul30 Jul 29 '18 at 15:27