前提・実現したいこと
購買データのデータ加工を
Jupyter Notebook上でしています。
object型のIDとdatetime型の日付でできた
リストを昇順でソートしたいと思ったのですが
以下のエラーが起き実行できませんでした。
発生している問題・エラーメッセージ
TypeError Traceback (most recent call last) <ipython-input-22-e2c1bcf11d28> in <module> 1 #昇順に並べ替え ----> 2 times.sort() c:\users\01029944\kawamura\lib\site-packages\pandas\core\ops\__init__.py in wrapper(self, other, axis) 1177 other = Timestamp(other) 1178 -> 1179 res_values = dispatch_to_index_op(op, self, other, pd.DatetimeIndex) 1180 1181 return self._constructor(res_values, index=self.index, name=res_name) c:\users\01029944\kawamura\lib\site-packages\pandas\core\ops\__init__.py in dispatch_to_index_op(op, left, right, index_class) 628 left_idx = left_idx._shallow_copy(freq=None) 629 try: --> 630 result = op(left_idx, right) 631 except NullFrequencyError: 632 # DatetimeIndex and TimedeltaIndex with freq == None raise ValueError c:\users\lib\site-packages\pandas\core\indexes\datetimelike.py in wrapper(self, other) 130 other = other._values 131 --> 132 result = op(self._data, maybe_unwrap_index(other)) 133 return result 134 c:\users\lib\site-packages\pandas\core\arrays\datetimes.py in wrapper(self, other) 200 with np.errstate(all="ignore"): 201 result = ops._comp_method_OBJECT_ARRAY( --> 202 op, self.astype(object), other 203 ) 204 o_mask = isna(other) c:\users\lib\site-packages\pandas\core\ops\__init__.py in _comp_method_OBJECT_ARRAY(op, x, y) 1065 y = y.values 1066 -> 1067 result = libops.vec_compare(x, y, op) 1068 else: 1069 result = libops.scalar_compare(x, y, op) pandas\_libs\ops.pyx in pandas._libs.ops.vec_compare() TypeError: '<' not supported between instances of 'Timestamp' and 'str'
該当のソースコード
Python
1 2#まずは、「回数=同ID×同日×同店」のリストを作成する 3times = [whole['cst_id'], whole['date_purchase_c'], whole['Store_code']] 4times
[0 3144054828245
1 3144054828245
2 3144054886139
3 3144054886139
4 3144054886139
...
959000 3149006447374
959001 3149006447374
959002 3149007331709
959003 3149007673303
959004 3149007673303
Name: cst_id, Length: 959005, dtype: object, 0 2017-06-14
1 2018-09-22
2 2017-06-18
3 2017-06-18
4 2018-07-21
...
959000 2018-09-29
959001 2018-09-29
959002 2018-09-29
959003 2018-09-30
959004 2018-09-30
Name: date_purchase_c, Length: 959005, dtype: datetime64[ns], 0 3001
1 5026
2 3001
3 3001
4 3042
...
959000 6116
959001 6116
959002 6116
959003 6116
959004 6116
Name: Store_code, Length: 959005, dtype: object]
Python
1#昇順に並べ替え 2times.sort()
試したこと
int()とかをリストを作る段階で使い、
ID等の型を変えようとしましたが、
TypeError等が起き、
上手くいきませんでした。
また、sort_indexやsort_valuesも試してみたのですが、
AttributeError: 'list' object has no attribute 'sort_index'
みたいなエラーが起きてうまくいきません。。。
これはリストの作り方から変えたほうがいいのでしょうか?
回答1件
あなたの回答
tips
プレビュー