0

I have a data frame column, named date with the following values:

         date
0    201801010000
1    201801010050
2    201801010150
3    201801010250
4    201801010300
5    201801010350
6    201801010450
7    201801010550
8    201801010600
9    201801010650

I want to convert it to date and time, so I used the following code:

df2['date'] = pd.datetime(df2['date'], format='%Y%m%d%H%M')

But, its giving me an error:

TypeError: an integer is required

I thought that I need to convert the column into integer so, I converted it as:

df2["date"] = df2.date.astype(int)

But, still I am getting the same error. Could anyone tell me why am I getting the error?

cs95
  • 379,657
  • 97
  • 704
  • 746
Mala Pokhrel
  • 143
  • 2
  • 2
  • 19
  • 1
    `pd.to_datetime(df['GMT'].astype(str), format='%Y%m%d%H%M')` – cs95 Jun 12 '19 at 03:54
  • @cs95 Its giving me error `KeyError: 'GMT'` – Mala Pokhrel Jun 12 '19 at 03:57
  • That is literally the column name you've pasted in your question. Replace "GMT" with "date", or whatever. It's your data. – cs95 Jun 12 '19 at 03:58
  • @cs95 After using the code as you mentioned, I am getting different error `ValueError: Buffer has wrong number of dimensions (expected 1, got 2) ` – Mala Pokhrel Jun 12 '19 at 04:21
  • 1
    The fix would be: `pd.to_datetime(df['date'].astype(str), format='%Y%m%d%H%M', errors='coerce')` – cs95 Jun 12 '19 at 04:22
  • @cs95 After using the code my data type is still object, shouldn't it be converted to dtype: datetime64 as https://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.to_datetime.html ? – Mala Pokhrel Jun 12 '19 at 04:51
  • It will be, if you assign it back to a column. – cs95 Jun 12 '19 at 04:53
  • 1
    Not required, I've given you all the information you need to solve this problem yourself. – cs95 Jun 12 '19 at 04:55
  • I have assigned it as `df2['date']=pd.to_datetime(df2['date'].astype(str), format='%Y%m%d%H%M',errors='coerce')`, but it is still an object datatype – Mala Pokhrel Jun 12 '19 at 05:05

0 Answers0