2017-11-28 7 views
0

は(私はthis答えを読んだことがあるが、もはや動作していないようです)保存パンダDATAFRAME(?既存のSOの答えは動作しません)

は、たとえばexample_1.csvのために考えてみましょう:

timestamp,temp 
21-Jun-2017 00:36:49.539000,22 
21-Jun-2017 00:36:49.633000,22 
21-Jun-2017 00:36:49.633000,22 
21-Jun-2017 00:37:03.055000,42 
21-Jun-2017 00:37:03.164000,22 
21-Jun-2017 00:37:03.164000,22 
21-Jun-2017 00:37:12.680000,42 
21-Jun-2017 00:37:22.664000,42 
21-Jun-2017 00:37:22.664000,42 
21-Jun-2017 00:37:22.758000,42 
21-Jun-2017 00:37:22.758000,42 

です。これまで

import pandas 

>>> pandas.__version__ 
'0.21.0' 


example_df = pandas.read_csv('example_1.csv', index_col = 0) 
example_df.index = pandas.to_datetime(example_df.index, format = '%d-%b-%Y %H:%M:%S.%f') 

、とても良い:のpython 3.5で

>>> example_df.index 
DatetimeIndex(['2017-06-21 00:36:49.539000', '2017-06-21 00:36:49.633000', 
       '2017-06-21 00:36:49.633000', '2017-06-21 00:37:03.055000', 
       '2017-06-21 00:37:03.164000', '2017-06-21 00:37:03.164000', 
       '2017-06-21 00:37:12.680000', '2017-06-21 00:37:22.664000', 
       '2017-06-21 00:37:22.664000', '2017-06-21 00:37:22.758000', 
       '2017-06-21 00:37:22.758000'], 
       dtype='datetime64[ns]', name='timestamp', freq=None) 

しかし、その後、私は物事を保存する必要があります。

example_df.to_csv('example_2.csv', date_format = '%d-%b-%Y %H:%M:%S.%f') 
example_df_2 = pandas.read_csv('example_2.csv', index_col = 0) 

が、その後、私はexample_df_2年代に読んだときインデックスとして表示されません。 datetime64[ns]

>>> example_df_2.index 
Index(['21-Jun-2017 00:36:49.539000', '21-Jun-2017 00:36:49.633000', 
     '21-Jun-2017 00:36:49.633000', '21-Jun-2017 00:37:03.055000', 
     '21-Jun-2017 00:37:03.164000', '21-Jun-2017 00:37:03.164000', 
     '21-Jun-2017 00:37:12.680000', '21-Jun-2017 00:37:22.664000', 
     '21-Jun-2017 00:37:22.664000', '21-Jun-2017 00:37:22.758000', 
     '21-Jun-2017 00:37:22.758000'], 
     dtype='object', name='timestamp') 

また、これは助けにはならない:

>>> example_df_2.index.astype('datetime64[ns]') 
Traceback (most recent call last): 
    File "<stdin>", line 1, in <module> 
    File "/usr/local/lib/python3.5/dist-packages/pandas/core/indexes/base.py", line 1059, in astype 
    return Index(self.values.astype(dtype, copy=copy), name=self.name, 
ValueError: Error parsing datetime string "21-Jun-2017 00:37:22.758000" at position 3 

は今、このファイルには、弓削あると私はすぐに、後者のそれを読むことができる形式で保存する必要があります。私はそれを避けることができれば、日付のタイムスタンプを2回解析する必要はありません。

これを修正するにはどうすればよいですか? (または何が間違っ淫魔をやって?)

答えて

1

使用parse_datesパラメータ:

example_df_2 = pd.read_csv('example_2.csv', index_col = 0, parse_dates=True) 
example_df_2.index 

出力:

example_df = pd.read_csv('example_1.csv', index_col=0, parse_dates=True) 
example_df.index 

DatetimeIndex(['2017-06-21 00:36:49.539000', '2017-06-21 00:36:49.633000', 
       '2017-06-21 00:36:49.633000', '2017-06-21 00:37:03.055000', 
       '2017-06-21 00:37:03.164000', '2017-06-21 00:37:03.164000', 
       '2017-06-21 00:37:12.680000', '2017-06-21 00:37:22.664000', 
       '2017-06-21 00:37:22.664000', '2017-06-21 00:37:22.758000', 
       '2017-06-21 00:37:22.758000'], 
       dtype='datetime64[ns]', name='timestamp', freq=None) 

注意、あなたはまた、最初のインポートのためにこれを行うことができます

出力:

DatetimeIndex(['2017-06-21 00:36:49.539000', '2017-06-21 00:36:49.633000', 
       '2017-06-21 00:36:49.633000', '2017-06-21 00:37:03.055000', 
       '2017-06-21 00:37:03.164000', '2017-06-21 00:37:03.164000', 
       '2017-06-21 00:37:12.680000', '2017-06-21 00:37:22.664000', 
       '2017-06-21 00:37:22.664000', '2017-06-21 00:37:22.758000', 
       '2017-06-21 00:37:22.758000'], 
       dtype='datetime64[ns]', name='timestamp', freq=None) 
+0

ありがとうございました。しかし、それは本当に私が望むものではありません:それはまだ非常に遅く、 'parse_date'です。日時インデックスを直接読み取ることができる(再解析する必要はありません)方法でpandas dfを保存する方法はありますか? – user189035

+0

@ user189035いいえ、CSVはテキストファイルであり、すべてのテキストを何らかの形で解析する必要があるため、日時を直接保存することはできません。しかし、私は速い方法は、ファイルを読み込み、日付を解析しないで、 'df.index = pd.to_datetime(df.index、format = '%d-%b-%Y%H%:% M:%S.%f ') 'を返します。 –

+0

私がリンクしている答えを見てください。それはパンダ0.15何とかそれを行うことができるようです。 – user189035

関連する問題