-1

One of the columns of a CSV file is a list of timestamps. eg.,

[Timestamp('2015-01-15 16:37:00'), Timestamp('2016-04-25 16:37:00'), Timestamp('2017-08-20 16:37:00')]

When I am reading the CSV, I want this column to be read like a list of timestamps.

I am trying to write an apply function to convert the column which is string of list of timestamps as above to a list of DateTime timestamps using the ast library. However, I get the following error

ValueError: malformed node or string: <_ast.Call object at 0x0000023FBDC77748>

I have the reproducible code below

import ast
x = "[Timestamp('2015-01-15 16:37:00'), Timestamp('2016-04-25 16:37:00'), Timestamp('2017-08-20 16:37:00')]"
y = ast.literal_eval(x)

EDIT: As indicated in one of the answers below, I tried eval()

x = "[Timestamp('2015-01-15 16:37:00'), Timestamp('2016-04-25.16:37:00'), Timestamp('2017-08-20 16:37:00')]"
y = eval(x)

I get the following error:

y = eval(x)
  File "<string>", line 1, in <module>
NameError: name 'Timestamp' is not defined
Omar_Hafez
  • 272
  • 2
  • 13
skr
  • 452
  • 6
  • 21
  • 1
    Is this correct or you just missed the 't' in this line of code? y = as.literal_eval(x) – daniboy000 Aug 20 '19 at 21:02
  • Yes, that was a typo I fixed it – skr Aug 20 '19 at 21:29
  • Your second Timestamp also differs between your first and the other two listing: `Timestamp('2016-04-25 16:37:00')` has a space between the date and the time while `Timestamp('2016-04-25.16:37:00')` has a dot. I assume this is a typo as well. – skymon Aug 21 '19 at 21:43

1 Answers1

2

ast.literal_eval does not allow for the construction of objects (i.e. your Timestamp call).

You could replace it with:

y = eval(x)

This should work, but is not without disadvantages. Other than that, you could for example do some regex parsing as described here, but that might create some rather lengthy code. Might be worth it, though, depending on your application.

Edit: Your overall code could look like this, including a definition for Timestamp():

from datetime import datetime

def Timestamp(timestr): return datetime.strptime(timestr,'%Y-%m-%d %H:%M:%S')

x = "[Timestamp('2015-01-15 16:37:00'), Timestamp('2016-04-25 16:37:00'), Timestamp('2017-08-20 16:37:00')]"
y = eval(x)

print(y)

This prints:

[datetime.datetime(2015, 1, 15, 16, 37), datetime.datetime(2016, 4, 25, 16, 37), datetime.datetime(2017, 8, 20, 16, 37)]

skymon
  • 850
  • 1
  • 12
  • 19
  • 2
    Note: Be careful when using eval for example if you have imported os, if there is a value in the csv of for example `os.system('rm -rf *')`, this could cause your drive to be wiped See: https://www.programiz.com/python-programming/methods/built-in/eval – Phillip Aug 20 '19 at 21:20
  • I removed the accepted flag as this did not work, even for the code I presented. I get an error. Please see the edited question. – skr Aug 21 '19 at 18:36
  • 1
    I just assumed you had that function defined, since your problem just seemed to be with the "malformed node" part. But you can of course just define it, e.g. like this: `def Timestamp(timestr): return datetime.strptime(timestr,'%Y-%m-%d %H:%M:%S')`. Make sure that you have datetime imported, see edited answer for complete code. – skymon Aug 21 '19 at 21:40