TypeError: Unrecognized value type: <class ‘str‘> ParserError: Unknown string format
Posted Data+Science+Insight
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了TypeError: Unrecognized value type: <class ‘str‘> ParserError: Unknown string format相关的知识,希望对你有一定的参考价值。
TypeError: Unrecognized value type: <class 'str'> ParserError: Unknown string format
目录
TypeError: Unrecognized value type: ParserError: Unknown string format
问题:
import pandas as pd
# Define Panda Series
times = pd.Series(["2021-01-25", "2021/01/08", "2021", "Hello World", "Jan 4th, 2022"])
# Print Series
print("Series: \\n", times, "\\n")
# Convert Series to datetime
print("datetime: \\n", pd.to_datetime(times))
解决:
import pandas as pd
# Define Panda Series
times = pd.Series(["2021-01-25", "2021/01/08", "2021", "Hello World", "Jan 4th, 2022"])
# Print Series
print("Series: \\n", times, "\\n")
# Convert Series to datetime
print("datetime: \\n", pd.to_datetime(times, errors = 'coerce'))
Series: 0 2021-01-25 1 2021/01/08 2 2021 3 Hello World 4 Jan 4th, 2022 dtype: object datetime: 0 2021-01-25 1 2021-01-08 2 2021-01-01 3 NaT 4 2022-01-04 dtype: datetime64[ns]
完整错误:
Series: 0 2021-01-25 1 2021/01/08 2 2021 3 Hello World 4 Jan 4th, 2022 dtype: object
--------------------------------------------------------------------------- TypeError Traceback (most recent call last) D:\\anaconda\\lib\\site-packages\\pandas\\core\\arrays\\datetimes.py in objects_to_datetime64ns(data, dayfirst, yearfirst, utc, errors, require_iso8601, allow_object) 2084 try: -> 2085 values, tz_parsed = conversion.datetime_to_datetime64(data) 2086 # If tzaware, these values represent unix timestamps, so we pandas\\_libs\\tslibs\\conversion.pyx in pandas._libs.tslibs.conversion.datetime_to_datetime64() TypeError: Unrecognized value type: <class 'str'> During handling of the above exception, another exception occurred: ParserError Traceback (most recent call last) <ipython-input-1-a11a96b9d90d> in <module> 8 9 # Convert Series to datetime ---> 10 print("datetime: \\n", pd.to_datetime(times)) D:\\anaconda\\lib\\site-packages\\pandas\\core\\tools\\datetimes.py in to_datetime(arg, errors, dayfirst, yearfirst, utc, format, exact, unit, infer_datetime_format, origin, cache) 803 result = arg.map(cache_array) 804 else: --> 805 values = convert_listlike(arg._values, format) 806 result = arg._constructor(values, index=arg.index, name=arg.name) 807 elif isinstance(arg, (ABCDataFrame, abc.MutableMapping)): D:\\anaconda\\lib\\site-packages\\pandas\\core\\tools\\datetimes.py in _convert_listlike_datetimes(arg, format, name, tz, unit, errors, infer_datetime_format, dayfirst, yearfirst, exact) 470 errors=errors, 471 require_iso8601=require_iso8601, --> 472 allow_object=True, 473 ) 474 D:\\anaconda\\lib\\site-packages\\pandas\\core\\arrays\\datetimes.py in objects_to_datetime64ns(data, dayfirst, yearfirst, utc, errors, require_iso8601, allow_object) 2088 return values.view("i8"), tz_parsed 2089 except (ValueError, TypeError): -> 2090 raise e 2091 2092 if tz_parsed is not None: D:\\anaconda\\lib\\site-packages\\pandas\\core\\arrays\\datetimes.py in objects_to_datetime64ns(data, dayfirst, yearfirst, utc, errors, require_iso8601, allow_object) 2079 dayfirst=dayfirst, 2080 yearfirst=yearfirst, -> 2081 require_iso8601=require_iso8601, 2082 ) 2083 except ValueError as e: pandas\\_libs\\tslib.pyx in pandas._libs.tslib.array_to_datetime() pandas\\_libs\\tslib.pyx in pandas._libs.tslib.array_to_datetime() pandas\\_libs\\tslib.pyx in pandas._libs.tslib.array_to_datetime_object() pandas\\_libs\\tslib.pyx in pandas._libs.tslib.array_to_datetime_object() pandas\\_libs\\tslibs\\parsing.pyx in pandas._libs.tslibs.parsing.parse_datetime_string() D:\\anaconda\\lib\\site-packages\\dateutil\\parser\\_parser.py in parse(timestr, parserinfo, **kwargs) 1372 return parser(parserinfo).parse(timestr, **kwargs) 1373 else: -> 1374 return DEFAULTPARSER.parse(timestr, **kwargs) 1375 1376 D:\\anaconda\\lib\\site-packages\\dateutil\\parser\\_parser.py in parse(self, timestr, default, ignoretz, tzinfos, **kwargs) 647 648 if res is None: --> 649 raise ParserError("Unknown string format: %s", timestr) 650 651 if len(res) == 0: ParserError: Unknown string format: Hello World
以上是关于TypeError: Unrecognized value type: <class ‘str‘> ParserError: Unknown string format的主要内容,如果未能解决你的问题,请参考以下文章
Linux - Python的虚拟环境配置的坑 virtualenv: error: unrecognized arguments: --no-site-packages
解决JSON解析报错JsonParseException_ Unrecognized token ‘zxx‘_ was expecting (‘true‘, ‘false‘ or ‘null‘)
解决JSON解析报错JsonParseException_ Unrecognized token ‘zxx‘_ was expecting (‘true‘, ‘false‘ or ‘null‘)
猫鼬更新总是给我 TypeError: Cannot set property '__v' of undefined