質問編集履歴
1
エラーメッセージの追記
test
CHANGED
File without changes
|
test
CHANGED
@@ -12,7 +12,7 @@
|
|
12
12
|
|
13
13
|
のように変換したいです
|
14
14
|
|
15
|
-
|
15
|
+
|
16
16
|
|
17
17
|
df['年月'] = pd.to_datetime(df['年月'], format = '%Y%m')
|
18
18
|
|
@@ -49,3 +49,103 @@
|
|
49
49
|
解決策を教えてほしいです。
|
50
50
|
|
51
51
|
(エクセルデータの前処理が必要なのでしょうか?あるいはpandasで数字列をうまく処理できるのでしょうか??)
|
52
|
+
|
53
|
+
|
54
|
+
|
55
|
+
### 発生している問題・エラーメッセージ
|
56
|
+
|
57
|
+
TypeError Traceback (most recent call last)
|
58
|
+
|
59
|
+
~\anaconda3\lib\site-packages\pandas\core\tools\datetimes.py in _convert_listlike_datetimes(arg, format, name, tz, unit, errors, infer_datetime_format, dayfirst, yearfirst, exact)
|
60
|
+
|
61
|
+
449 try:
|
62
|
+
|
63
|
+
--> 450 values, tz = conversion.datetime_to_datetime64(arg)
|
64
|
+
|
65
|
+
451 dta = DatetimeArray(values, dtype=tz_to_dtype(tz))
|
66
|
+
|
67
|
+
|
68
|
+
|
69
|
+
pandas\_libs\tslibs\conversion.pyx in pandas._libs.tslibs.conversion.datetime_to_datetime64()
|
70
|
+
|
71
|
+
|
72
|
+
|
73
|
+
TypeError: Unrecognized value type: <class 'str'>
|
74
|
+
|
75
|
+
|
76
|
+
|
77
|
+
During handling of the above exception, another exception occurred:
|
78
|
+
|
79
|
+
|
80
|
+
|
81
|
+
ValueError Traceback (most recent call last)
|
82
|
+
|
83
|
+
<ipython-input-7-41d172471441> in <module>
|
84
|
+
|
85
|
+
----> 1 df['年月'] = pd.to_datetime(df['年月'].astype(str), format = '%Y%m')
|
86
|
+
|
87
|
+
2 df['年月']
|
88
|
+
|
89
|
+
|
90
|
+
|
91
|
+
~\anaconda3\lib\site-packages\pandas\core\tools\datetimes.py in to_datetime(arg, errors, dayfirst, yearfirst, utc, format, exact, unit, infer_datetime_format, origin, cache)
|
92
|
+
|
93
|
+
797 result = result.tz_localize(tz)
|
94
|
+
|
95
|
+
798 elif isinstance(arg, ABCSeries):
|
96
|
+
|
97
|
+
--> 799 cache_array = _maybe_cache(arg, format, cache, convert_listlike)
|
98
|
+
|
99
|
+
800 if not cache_array.empty:
|
100
|
+
|
101
|
+
801 result = arg.map(cache_array)
|
102
|
+
|
103
|
+
|
104
|
+
|
105
|
+
~\anaconda3\lib\site-packages\pandas\core\tools\datetimes.py in _maybe_cache(arg, format, cache, convert_listlike)
|
106
|
+
|
107
|
+
168 unique_dates = unique(arg)
|
108
|
+
|
109
|
+
169 if len(unique_dates) < len(arg):
|
110
|
+
|
111
|
+
--> 170 cache_dates = convert_listlike(unique_dates, format)
|
112
|
+
|
113
|
+
171 cache_array = Series(cache_dates, index=unique_dates)
|
114
|
+
|
115
|
+
172 return cache_array
|
116
|
+
|
117
|
+
|
118
|
+
|
119
|
+
~\anaconda3\lib\site-packages\pandas\core\tools\datetimes.py in _convert_listlike_datetimes(arg, format, name, tz, unit, errors, infer_datetime_format, dayfirst, yearfirst, exact)
|
120
|
+
|
121
|
+
452 return DatetimeIndex._simple_new(dta, name=name)
|
122
|
+
|
123
|
+
453 except (ValueError, TypeError):
|
124
|
+
|
125
|
+
--> 454 raise e
|
126
|
+
|
127
|
+
455
|
128
|
+
|
129
|
+
456 if result is None:
|
130
|
+
|
131
|
+
|
132
|
+
|
133
|
+
~\anaconda3\lib\site-packages\pandas\core\tools\datetimes.py in _convert_listlike_datetimes(arg, format, name, tz, unit, errors, infer_datetime_format, dayfirst, yearfirst, exact)
|
134
|
+
|
135
|
+
415 if result is None:
|
136
|
+
|
137
|
+
416 try:
|
138
|
+
|
139
|
+
--> 417 result, timezones = array_strptime(
|
140
|
+
|
141
|
+
418 arg, format, exact=exact, errors=errors
|
142
|
+
|
143
|
+
419 )
|
144
|
+
|
145
|
+
|
146
|
+
|
147
|
+
pandas\_libs\tslibs\strptime.pyx in pandas._libs.tslibs.strptime.array_strptime()
|
148
|
+
|
149
|
+
|
150
|
+
|
151
|
+
ValueError: unconverted data remains: .0
|