reihaneh roshan
reihaneh roshan

Reputation: 11

ValueError: time data does not match format '%m/%d/%Y %H:%M:%S.%f'

I am writing this code to change the type of "Gmt time" column. But, I give "ValueError: time data does not match format ". How could I convert to float or someother types except Object and string?

My code is:

path= '/content/drive/MyDrive/H1_ASK_Raw.csv'
dateparse = lambda dates: pd.datetime.strptime(dates, '%m/%d/%Y %H:%M:%S.%f')
df = pd.read_csv(path,sep=',', index_col='Date', parse_dates=['Gmt time'], date_parser=dateparse).fillna(0)
df.head()

I get the below Error:

TypeError                                 Traceback (most recent call last)
/usr/local/lib/python3.7/dist-packages/pandas/io/parsers/base_parser.py in converter(*date_cols)
1038                 result = tools.to_datetime(
-> 1039                     date_parser(*date_cols), errors="ignore", cache=cache_dates
1040                 )

                                 18 frames
TypeError: strptime() argument 1 must be str, not numpy.ndarray

 During handling of the above exception, another exception occurred:

 ValueError                                Traceback (most recent call last)
 ValueError: time data '05.05.2003 00:00:00.000' does not match format '%m/%d/%Y %H:%M:%S'

 During handling of the above exception, another exception occurred:

 ValueError                                Traceback (most recent call last)
 /usr/lib/python3.7/_strptime.py in _strptime(data_string, format)
 357     if not found:
 358         raise ValueError("time data %r does not match format %r" %
 --> 359                          (data_string, format))
 360     if len(data_string) != found.end():
 361         raise ValueError("unconverted data remains: %s" %

 ValueError: time data '05.05.2003 00:00:00.000' does not match format '%m/%d/%Y %H:%M:%S.%f'

Upvotes: 0

Views: 9463

Answers (1)

make 2 argument

stock['Date'] = pd.to_datetime(stock['Date']).dt.strftime('%m/%d/%y')

stock['Date_ts'] = pd.to_datetime(stock['Date_ts']).dt.strftime('%m-%d-%y')

Upvotes: 0

Related Questions