TypeError: Argument 'date_string' has incorrect type (expected str, got NavigableString) ํด๊ฒฐ๋ฒ
๐ ์ค๋ฅ
์น ์คํฌ๋ํ์ผ๋ก ๋ ์ง ๋ฐ์ดํฐ๋ฅผ ์์งํ์๋ค. ๋ฌธ์ํ์ ๊ทธ๋๋ก pandas DataFrame์ ๋ฃ์๋ค.
data
Out[216]:
['2020.08.26 10:14',
'2020.08.26 11:47',
'2020.08.26 13:40',
'2020.08.26 12:17',
'2020.08.26 12:08',
'2020.08.27 13:15',
'2020.08.26 13:57',
'2020.08.26 09:18',
'2020.08.26 09:04',
'2020.08.26 10:04']
df = pd.DataFrame({'datetime' : data})
df
Out[218]:
datetime
0 2020.08.26 10:14
1 2020.08.26 11:47
2 2020.08.26 13:40
3 2020.08.26 12:17
4 2020.08.26 12:08
5 2020.08.27 13:15
6 2020.08.26 13:57
7 2020.08.26 09:18
8 2020.08.26 09:04
9 2020.08.26 10:04
datetime ์ด์ ๋ค์๊ณผ ๊ฐ์ด object ํ์์ผ๋ก ๋์ด์๋ค.
df.info()
<class 'pandas.core.frame.DataFrame'>
RangeIndex: 10 entries, 0 to 9
Data columns (total 1 columns):
# Column Non-Null Count Dtype
--- ------ -------------- -----
0 datetime 10 non-null object
dtypes: object(1)
memory usage: 208.0+ bytes
๊ทธ๋ฐ๋ฐ ์ด ์ด์ datetime ํ์์ผ๋ก ๋ฐ๊พธ๋ ๊ณผ์ ์์ type error๊ฐ ๋ฐ์ํ๋ค.
df.datetime = pd.to_datetime(df.datetime, format = '%Y.%m.%d %H:%M')
Traceback (most recent call last):
File "pandas\_libs\tslib.pyx", line 605, in pandas._libs.tslib.array_to_datetime
TypeError: Expected unicode, got NavigableString
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "<ipython-input-220-f80b1cd7e09c>", line 1, in <module>
df.datetime = pd.to_datetime(df.datetime, format = '%Y.%m.%d %H:%M')
File "C:\ProgramData\Anaconda3\lib\site-packages\pandas\core\tools\datetimes.py", line 728, in to_datetime
values = convert_listlike(arg._values, format)
File "C:\ProgramData\Anaconda3\lib\site-packages\pandas\core\tools\datetimes.py", line 440, in _convert_listlike_datetimes
result, tz_parsed = objects_to_datetime64ns(
File "C:\ProgramData\Anaconda3\lib\site-packages\pandas\core\arrays\datetimes.py", line 1848, in objects_to_datetime64ns
result, tz_parsed = tslib.array_to_datetime(
File "pandas\_libs\tslib.pyx", line 481, in pandas._libs.tslib.array_to_datetime
File "pandas\_libs\tslib.pyx", line 703, in pandas._libs.tslib.array_to_datetime
File "pandas\_libs\tslib.pyx", line 828, in pandas._libs.tslib.array_to_datetime_object
TypeError: Argument 'date_string' has incorrect type (expected str, got NavigableString)
NavigableString ๊ฐ์ฒด๋ ํ๊ทธ ์์ ํ ์คํธ๋ค. ์ด๋ฅผ pandas๊ฐ str์ผ๋ก ์ธ์ํ์ง ๋ชปํ๋ ๊ฒ์ผ๋ก ํ์ธ๋๋ค.
type(df.datetime[0])
Out[226]: bs4.element.NavigableString
๐ก ํด๊ฒฐ
์ด๋ฅผ ํด๊ฒฐํ๊ธฐ ์ํด, DataFrame์ ๋ฃ๊ธฐ ์ str๋ก ๋ณํํ๋ ์์ ์ ๊ฑฐ์ณ์ฃผ๋ฉด ๋๋ค.
df = pd.DataFrame({'datetime' : [str(i) for i in data]}) โญโญโญ
Dtype์ ์ด์ ๊ณผ ๋ง์ฐฌ๊ฐ์ง๋ก object ์ง๋ง, ๊ฐ ๋ฐ์ดํฐ์ ํ์ ์ str ์์ ํ์ธํ ์ ์๋ค.
df.info()
<class 'pandas.core.frame.DataFrame'>
RangeIndex: 10 entries, 0 to 9
Data columns (total 1 columns):
# Column Non-Null Count Dtype
--- ------ -------------- -----
0 datetime 10 non-null object
dtypes: object(1)
memory usage: 208.0+ bytes
type(df.datetime[0])
Out[229]: str
๊ทธ๋ฆฌ๊ณ to_datetime ์ ์ ์ฉํ๋ฉด ๋ฌธ์ ์์ด ์ํ๋๋ค.
df.datetime = pd.to_datetime(df.datetime, format = '%Y.%m.%d %H:%M')
df.datetime
Out[231]:
0 2020-08-26 10:14:00
1 2020-08-26 11:47:00
2 2020-08-26 13:40:00
3 2020-08-26 12:17:00
4 2020-08-26 12:08:00
5 2020-08-27 13:15:00
6 2020-08-26 13:57:00
7 2020-08-26 09:18:00
8 2020-08-26 09:04:00
9 2020-08-26 10:04:00
Name: datetime, dtype: datetime64[ns]
<class 'pandas.core.frame.DataFrame'>
RangeIndex: 10 entries, 0 to 9
Data columns (total 1 columns):
# Column Non-Null Count Dtype
--- ------ -------------- -----
0 datetime 10 non-null datetime64[ns]
dtypes: datetime64[ns](1)
memory usage: 208.0 bytes