これは比較的簡単です。Pandas Dataframeカラムの操作はインデックスを使用
A B C
1/8/2017 1/11/2017 1/20/2017 1/25/2017
1/9/2017 1/11/2017 1/20/2017 1/25/2017
1/10/2017 1/11/2017 1/20/2017 1/25/2017
1/11/2017 1/20/2017 1/25/2017 1/31/2017
1/12/2017 1/20/2017 1/25/2017 1/31/2017
1/13/2017 1/20/2017 1/25/2017 1/31/2017
私はDates.indexと日付の差を利用したいと思います。私はパンダのデータフレーム(日付)を持っています。
A B C
1/8/2017 3 12 17
1/9/2017 2 11 16
1/10/2017 1 10 15
1/11/2017 9 14 20
1/12/2017 8 13 19
1/13/2017 7 12 18
当然のことながら、私はこの試みた:出力がそうするようなものだ
Dates - Dates.index
をしかし、私はこの素敵なTypeError例外受け取る:代わりに
TypeError: Could not operate DatetimeIndex...with block values ufunc subtract cannot use operands with types dtype('<M8[ns]') and dtype('O')
を、私はループには書かれてきました列ごとに移動しますが、それはちょうど愚かなようです。誰もこれを行うためにpythonic方法を提案することはできますか?
EDIT
In [1]: import pandas as pd
import numpy as np
import datetime
dates = pd.date_range('20170108',periods=6)
df = pd.DataFrame(np.empty([len(dates),3]),index=dates,columns=list('ABC'))
df['A'].loc[0:3] = datetime.date(2017, 1, 11)
df['B'].loc[0:3] = datetime.date(2017, 1, 20)
df['C'].loc[0:3] = datetime.date(2017, 1, 25)
df['A'].loc[3:6] = datetime.date(2017, 1, 20)
df['B'].loc[3:6] = datetime.date(2017, 1, 25)
df['C'].loc[3:6] = datetime.date(2017, 1, 31)
In [2]: print(df)
A B C
2017-01-08 2017-01-11 2017-01-20 2017-01-25
2017-01-09 2017-01-11 2017-01-20 2017-01-25
2017-01-10 2017-01-11 2017-01-20 2017-01-25
2017-01-11 2017-01-20 2017-01-25 2017-01-31
2017-01-12 2017-01-20 2017-01-25 2017-01-31
2017-01-13 2017-01-20 2017-01-25 2017-01-31
In [3]: df = df.sub(df.index.to_series(),axis=0)
ValueError: operands could not be broadcast together with shapes (18,) (6,)
これはうまく動作します。 – trob