Ian Cornejo
Ian Cornejo

Reputation: 49

Swapping Str to Datetime in a CSV (Python)

I have a data set named aero I am trying to change the string dates in a given column to datetime This is the format of the strings:

In: aero['Date Local'].unique()
Out: array(['2000-01-01', '2000-01-02', '2000-01-03', ..., '2016-03-04',
   '2016-03-05', '2016-03-06'], dtype=object)

So correct me if I'm wrong, but that looks like a mutable list of strings here is the code I tried:

for stuff in aero['Date Local']:
aero['Date Local'][stuff] = datetime.datetime.strptime(stuff, "%Y-%m-%d")  

which yielded the error:

ValueError: ['2' '0' '0' '0' '-' '0' '1' '-' '0' '1'] not contained in the index

I've tried figuring out what that means, but to no avail. Could someone help me help me switch these strings to datetimes?

Upvotes: 1

Views: 54

Answers (1)

jpp
jpp

Reputation: 164843

You are defining a new variable stuff each iteration of your loop. That's not what you want. You can just use astype to convert your array to datetime:

A = np.array(['2000-01-01', '2000-01-02', '2000-01-03', '2016-03-04',
              '2016-03-05', '2016-03-06'], dtype=object)

res = A.astype('datetime64[ns]')

print(res)

array(['2000-01-01T00:00:00.000000000', '2000-01-02T00:00:00.000000000',
       '2000-01-03T00:00:00.000000000', '2016-03-04T00:00:00.000000000',
       '2016-03-05T00:00:00.000000000', '2016-03-06T00:00:00.000000000'],
      dtype='datetime64[ns]')

Equivalently, if you have Pandas you can use pd.to_datetime:

import pandas as pd

res = pd.to_datetime(A).values  # .values extracts NumPy array

Therefore, assuming aero is a Pandas dataframe, you can use:

aero['Date Local'] = pd.to_datetime(aero['Date Local'])

Upvotes: 1

Related Questions