質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.48%
Python 3.x

Python 3はPythonプログラミング言語の最新バージョンであり、2008年12月3日にリリースされました。

Q&A

解決済

1回答

2389閲覧

objectとdatetimeでできたリストはソートできない???

Pablito

総合スコア71

Python 3.x

Python 3はPythonプログラミング言語の最新バージョンであり、2008年12月3日にリリースされました。

0グッド

0クリップ

投稿2019/08/14 05:40

編集2019/08/14 06:40

前提・実現したいこと

購買データのデータ加工を
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'
みたいなエラーが起きてうまくいきません。。。

これはリストの作り方から変えたほうがいいのでしょうか?

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

meg_

2019/08/14 06:02

timesはpandas.Seriesのリストですか?
Pablito

2019/08/14 06:06

meg_さんご質問ありがとうございます。 timesはdataframeのリストと思っています。 whole.info() <class 'pandas.core.frame.DataFrame'> Int64Index: 959005 entries, 0 to 959004 Data columns (total 76 columns):
meg_

2019/08/14 06:14

アウトプットとして欲しいのはどのようなものでしょうか? list型のsort()は型が違うとエラーが出ますね。
Pablito

2019/08/14 06:19

'cst_id'ごとに'date_purchase_c'と'Store_code'を 昇順に並べ替えたリスト、 で伝わりますでしょうか?
Pablito

2019/08/14 06:21

同じcst_idごとに、同じdate_purchase_c、Store_codeでまとめたくて、 これをまとめた上で、昇順に並べ替えたいのです。 上手く伝えられないのですが理解できますか?
meg_

2019/08/14 07:19

DataFrameのgroupby()で実現できそうな気がします。 元データが分からないので確かではありませんが、試してみてください。
guest

回答1

0

自己解決

よくわからなかったので、最終pd.DataFrameを使いました。

Python

1times = pd.DataFrame(whole[['cst_id', 'date_purchase_c', 'Store_code']]) 2times

Outputはこんな感じになり

cst_id      date_purchase_c Store_code
0 3144054828245 2017-06-14   3001
1 3144054828245 2018-09-22   5026
2 3144054886139 2017-06-18   3001
3 3144054886139 2017-06-18   3001
4 3144054886139 2018-07-21   3042
5 3144054886139 2018-10-28   3042
6 3144055008531 2017-06-11   3001
7 3144055018841 2017-06-13   3001
8 3144055018841 2017-10-28   3220
9 3144055018841 2018-03-15   3001

意外とsortしなくても
idごと購入日が早い順に並んでいる気がします。
どう思われますか?

投稿2019/08/14 07:25

Pablito

総合スコア71

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.48%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問