0

The way I am doing this looks inefficient, so I figured that there had to be a better way that I am just not seeing.

Current approach:

dates = pd.DataFrame(['2019-10-15', '2019-10-15', '2017-05-24', '2019-11-01', '2019-11-01',
     '2019-11-01', '2019-11-01', '2019-11-01', '2020-01-11', '2019-11-01'], columns=['string'])
dates['timestamp'] = [pd.Timestamp(x) for x in dates['string']]
Mark Graph
  • 4,969
  • 6
  • 25
  • 37

2 Answers2

1

The following code did the trick for me:

Since I am using inbuilt function and it is a vectorized operation. This would be relatively faster.

dates['timestamp'] = pd.to_datetime(dates['string'])

oreopot
  • 3,392
  • 2
  • 19
  • 28
0

You can use the apply function if you don't want to use a python list comprehension

dates['timestamp'] = dates['string'].apply(pd.Timestamp)

Won't be all that much faster but is a little cleaner imo

Milan
  • 344
  • 1
  • 10