私はあなたがdict
によってrename
とduplicated index
をマップ必要だと思う:
print (df)
a b c
timestamp
2013-10-06 01:00:00 1 NaN NaN
2013-10-06 01:30:00 2 NaN NaN
2013-10-06 01:00:00 3 NaN NaN
2013-10-06 01:30:00 4 NaN NaN
2012-10-08 01:30:00 5 NaN NaN
2013-10-10 01:00:00 6 NaN NaN
df1 = df[df.index.duplicated('first')]
d = dict(zip(df1.index, df1.shift(1,freq="H").index))
print (d)
{Timestamp('2013-10-06 01:00:00'): Timestamp('2013-10-06 02:00:00'),
Timestamp('2013-10-06 01:30:00'): Timestamp('2013-10-06 02:30:00')}
df = df.rename(index=d)
print (df)
a b c
timestamp
2013-10-06 02:00:00 1 NaN NaN
2013-10-06 02:30:00 2 NaN NaN
2013-10-06 02:00:00 3 NaN NaN
2013-10-06 02:30:00 4 NaN NaN
2012-10-08 01:30:00 5 NaN NaN
2013-10-10 01:00:00 6 NaN NaN
同様のソリューション:
idx = df.index[df.index.duplicated('first')]
d = dict(zip(idx, idx.to_series().shift(freq="H").index))
print (d)
{Timestamp('2013-10-06 01:00:00'): Timestamp('2013-10-06 02:00:00'),
Timestamp('2013-10-06 01:30:00'): Timestamp('2013-10-06 02:30:00')}
df = df.rename(index=d)
print (df)
a b c
timestamp
2013-10-06 02:00:00 1 NaN NaN
2013-10-06 02:30:00 2 NaN NaN
2013-10-06 02:00:00 3 NaN NaN
2013-10-06 02:30:00 4 NaN NaN
2012-10-08 01:30:00 5 NaN NaN
2013-10-10 01:00:00 6 NaN NaN
2013-10-06 02:30:00 8 NaN NaN
2012-10-08 01:30:00 9 NaN NaN
2013-10-10 01:00:00 10 NaN NaN
idx = df.index[df.index.duplicated('first')]
s = idx.to_series().shift(freq="H")
#swap index with values in Series
d = pd.Series(s.index.values, index = s.values).to_dict()
print (d)
{Timestamp('2013-10-06 01:00:00'): Timestamp('2013-10-06 02:00:00'),
Timestamp('2013-10-06 01:30:00'): Timestamp('2013-10-06 02:30:00')}
df = df.rename(index=d)
print (df)
a b c
timestamp
2013-10-06 02:00:00 1 NaN NaN
2013-10-06 02:30:00 2 NaN NaN
2013-10-06 02:00:00 3 NaN NaN
2013-10-06 02:30:00 4 NaN NaN
2012-10-08 01:30:00 5 NaN NaN
2013-10-10 01:00:00 6 NaN NaN
EDIT1:
あなたはcumcount
によって作成されたtimedeltas
とto_timedelta
を元のインデックスに追加する必要があります。
delta = pd.to_timedelta(df.groupby(level=0).cumcount(), unit='H')
print (delta)
timestamp
2013-10-06 01:00:00 00:00:00
2013-10-06 01:30:00 00:00:00
2013-10-06 01:00:00 01:00:00
2013-10-06 01:30:00 01:00:00
2012-10-08 01:30:00 00:00:00
2013-10-10 01:00:00 00:00:00
dtype: timedelta64[ns]
df.index = df.index + delta
print (df)
a b c
2013-10-06 01:00:00 1 NaN NaN
2013-10-06 01:30:00 2 NaN NaN
2013-10-06 02:00:00 3 NaN NaN
2013-10-06 02:30:00 4 NaN NaN
2012-10-08 01:30:00 5 NaN NaN
2013-10-10 01:00:00 6 NaN NaN
いいえ。最初の提案は、変更されたタイムスタンプのみを含むdf1を提供します(年の残りではありません)。 2番目の提案は、複製されたものだけでなく、dfの各タイムスタンプをシフトします。おかげさまで – doctorer
ありがとうございます。それはほとんどそこにあるが、それほどではない。これにより重複した値のインスタンスがすべて変更されたので、 '' 2012-10-07 02:00:00 'などの複製が作成されました。各タイムスタンプの_second_インスタンスのみを名前変更します。 – doctorer
理由を説明できますか? – jezrael