Dimi
Dimi

Reputation: 541

String does not contain a date

I have a DataFrame With This Column :

Mi_Meteo['Time_Instant'].head():

0    2013/11/14 17:00
1    2013/11/14 18:00
2    2013/11/14 19:00
3    2013/11/14 20:00
4    2013/11/14 21:00
Name: Time_Instant, dtype: object

After Doing Some Inspection This is What I realised :

Mi_Meteo['Time_Instant'].value_counts():

2013/12/09 02:00    33
2013/12/01 22:00    33
2013/12/11 10:00    33
2013/12/05 09:00    33
.
.
.
.
2013/11/16 02:00    21
2013/11/07 10:00    11
2013/11/17 22:00    11
DateTIme            3

So I striped it:

Mi_Meteo['Time_Instant'] = Mi_Meteo['Time_Instant'].str.rstrip('DateTIme')# Cause Otherwise I would get this Error When Converting : 'Unknown string format'

And Then I tried To Convert it :

Mi_Meteo['Time_Instant'] = pd.to_datetime(Mi_Meteo['Time_Instant'])

But I Get This Error:

String does not contain a date.

Any Suggestion Would Be Much Appreciated , Thank U all.

Upvotes: 2

Views: 13055

Answers (4)

Hamza
Hamza

Reputation: 6025

There are two possible solutions to this:

Either you can make the error disappear by using coerce in errors argument of pd.to_datetime() as follows:Mi_Meteo['Time_Instant'] = pd.to_datetime(Mi_Meteo['Time_Instant'], errors='coerce')

Or if you are interested to know which dates have the unparsable values, you can search for them by converting each value at a time as follows. Ths will work regardless of the type or format of the wrong value:

dates = []
wrong_dates = []
for i in Mi_Meteo['Time_Instant'],unique():
    try: 
        date = pd.to_datetime(i)
        dates.append(i)
    except:
        wrong_dates.append(i)

In the wrong_dates list you will have all the wrong values while in dates, all the right values

Upvotes: 0

R overflow
R overflow

Reputation: 1352

A bit late, why don't you use this:

Mi_Meteo['Time_Instant'] = pd.to_datetime(Mi_Meteo['Time_Instant'], errors='coerce')

In the pandas.to_datetime document a description of the 'errors' parameter:

errors{‘ignore’, ‘raise’, ‘coerce’}, default ‘raise’ If ‘raise’, then invalid parsing will raise an exception.

If ‘coerce’, then invalid parsing will be set as NaT.

If ‘ignore’, then invalid parsing will return the input.

Upvotes: 5

Grant Shannon
Grant Shannon

Reputation: 5055

I got the same error - it turns out that two of my dates were empty: ' '.

To find the row index of the problematic dates I used the following list comprehension:

badRows =   [n for n,x in enumerate(df['DATE'].tolist()) if x.strip() in ['']]

This returned a list, containing the indices of the rows in the 'DATE' column that were causing the problems:

[745672, 745673] 

Can then delete these rows in place:

df.drop(df.index[badRows],inplace=True)

Upvotes: 2

Declan
Declan

Reputation: 634

I'm having trouble reproducing your error, so I cannot be sure if this will fix the issue you have. If not then please try to provide a minimum sample of code/data that reproduces your error.

This is what I tried to reproduce your situation:

lzt = ['2013/11/16 02:00 ',
       '2013/11/07 10:00 ',
       '2013/11/17 22:00 ',
       'DateTIme',
       'DateTIme',
       'DateTIme']
ser = pd.Series(lzt)
ser = ser.str.rstrip('DateTIme')
ser = pd.to_datetime(ser)

But as I said I got no error, so either we have a different version of pandas or there's something else wrong with your data. Using rstrip leave some blank string data:

0    2013/11/16 02:00 
1    2013/11/07 10:00 
2    2013/11/17 22:00 
3                     
4                     
5                     

which for me gives NaT (not a time) when I run pd.to_datetime on it:

Out[34]: 
0   2013-11-16 02:00:00
1   2013-11-07 10:00:00
2   2013-11-17 22:00:00
3                   NaT
4                   NaT
5                   NaT
dtype: datetime64[ns]

I'd say it's better practice to remove the unwanted rows all together:

ser = ser[ser != 'DateTIme']

Out[39]: 
0   2013-11-16 02:00:00
1   2013-11-07 10:00:00
2   2013-11-17 22:00:00
dtype: datetime64[ns]

See if that works, otherwise please give enough information to reproduce the error.

Upvotes: 0

Related Questions