teratail header banner
teratail header banner
質問するログイン新規登録

質問編集履歴

1

エラーメッセージの追記

2021/04/22 00:36

投稿

takkucook
takkucook

スコア5

title CHANGED
File without changes
body CHANGED
@@ -5,7 +5,7 @@
5
5
  エクセル自体のデータ例:198604
6
6
  これを1986-04
7
7
  のように変換したいです
8
- ### 発生している問題・エラーメッセージ
8
+
9
9
  df['年月'] = pd.to_datetime(df['年月'], format = '%Y%m')
10
10
  df['年月']
11
11
 
@@ -23,4 +23,54 @@
23
23
  のようになってしまいます。
24
24
 
25
25
  解決策を教えてほしいです。
26
- (エクセルデータの前処理が必要なのでしょうか?あるいはpandasで数字列をうまく処理できるのでしょうか??)
26
+ (エクセルデータの前処理が必要なのでしょうか?あるいはpandasで数字列をうまく処理できるのでしょうか??)
27
+
28
+ ### 発生している問題・エラーメッセージ
29
+ TypeError Traceback (most recent call last)
30
+ ~\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)
31
+ 449 try:
32
+ --> 450 values, tz = conversion.datetime_to_datetime64(arg)
33
+ 451 dta = DatetimeArray(values, dtype=tz_to_dtype(tz))
34
+
35
+ pandas\_libs\tslibs\conversion.pyx in pandas._libs.tslibs.conversion.datetime_to_datetime64()
36
+
37
+ TypeError: Unrecognized value type: <class 'str'>
38
+
39
+ During handling of the above exception, another exception occurred:
40
+
41
+ ValueError Traceback (most recent call last)
42
+ <ipython-input-7-41d172471441> in <module>
43
+ ----> 1 df['年月'] = pd.to_datetime(df['年月'].astype(str), format = '%Y%m')
44
+ 2 df['年月']
45
+
46
+ ~\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)
47
+ 797 result = result.tz_localize(tz)
48
+ 798 elif isinstance(arg, ABCSeries):
49
+ --> 799 cache_array = _maybe_cache(arg, format, cache, convert_listlike)
50
+ 800 if not cache_array.empty:
51
+ 801 result = arg.map(cache_array)
52
+
53
+ ~\anaconda3\lib\site-packages\pandas\core\tools\datetimes.py in _maybe_cache(arg, format, cache, convert_listlike)
54
+ 168 unique_dates = unique(arg)
55
+ 169 if len(unique_dates) < len(arg):
56
+ --> 170 cache_dates = convert_listlike(unique_dates, format)
57
+ 171 cache_array = Series(cache_dates, index=unique_dates)
58
+ 172 return cache_array
59
+
60
+ ~\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)
61
+ 452 return DatetimeIndex._simple_new(dta, name=name)
62
+ 453 except (ValueError, TypeError):
63
+ --> 454 raise e
64
+ 455
65
+ 456 if result is None:
66
+
67
+ ~\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)
68
+ 415 if result is None:
69
+ 416 try:
70
+ --> 417 result, timezones = array_strptime(
71
+ 418 arg, format, exact=exact, errors=errors
72
+ 419 )
73
+
74
+ pandas\_libs\tslibs\strptime.pyx in pandas._libs.tslibs.strptime.array_strptime()
75
+
76
+ ValueError: unconverted data remains: .0