ValueError:时间数据与格式“%m/%d/%Y %H:%M:%S.%f”不匹配

问题描述 投票:0回答:1

我编写此代码是为了更改“Gmt time”列的类型。但是,我给出“ValueError:时间数据与格式不匹配”。如何转换为 float 或除 Object 和 string 之外的其他类型?

我的代码是:

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()

我收到以下错误:

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'
python python-datetime valueerror
1个回答
0
投票

提出2个论点

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')
© www.soinside.com 2019 - 2024. All rights reserved.