かなり簡単な質問です。連続した行間の営業日を取得するpandas
私が知っている:
df.diff()
は私の間の日数を与える、と私は
df.loc[df.Date.weekday == 4, 'Diff'] = 1
とモジョのいくつかの種類を行うことができます知っているしかし、それは最適ではありません。試しました
np.busday_count()
しかし、私はかなり理解していません。このエラーのコード例は次のとおりです。
In [36]: df = pd.DataFrame.from_dict({1: {'Date': '2016-01-01'}, 2: {'Date': '2016-01-02'}, 3: {'Date': '2016-01-03'}}, orient='index')
In [37]: df['Date'] = df.Date.astype('<M8[D]')
In [38]: np.busday_count(df.Date, df.Date.shift(1))
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
<ipython-input-38-07a4ae9a16f6> in <module>()
----> 1 np.busday_count(df.Date, df.Date.shift(1))
TypeError: Iterator operand 0 dtype could not be cast from dtype('<M8[ns]') to dtype('<M8[D]') according to the rule 'safe'
In [39]: df = pd.DataFrame.from_dict({1: {'Date': '2016-01-01'}, 2: {'Date': '2016-01-02'}, 3: {'Date': '2016-01-03'}}, orient='index')
In [40]: np.busday_count(df.Date, df.Date.shift(1))
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
<ipython-input-40-07a4ae9a16f6> in <module>()
----> 1 np.busday_count(df.Date, df.Date.shift(1))
TypeError: Iterator operand or requested dtype holds references, but the REFS_OK flag was not enabled
をnumpyの明示的な型キャストできれいに解決DTYPE 'datetime64 [D]は'一覧表示するには、変換せずに動作しますが: 'np.busday_count(df.Date.values.astype(' datetime64 [D] ')、df.Date.shift(1).fillna(df.Date).values.astype(' datetime64 [D] ') ) '。したがって、これは速くなければなりませんが、それほど読めるわけではありません。私はたぶんpd.Series/np.vectorsで一貫して動作するこれらの変換をすべて "my_busday_count"という関数で記述し、必要に応じて正しくNaTを出力します。 – hynekcer