Windstorm1981
Windstorm1981

Reputation: 2680

Pandas Issue subtracting a series from a dataframe column

I am subtracting a series from a dataframe column.

df

                daily_return    daily_weight

2003-01-01T          1.2            62
2003-01-02T          1.3            63
2003-01-03T          1.1            64
 ...

and series

                    Return
2003-01-01T          1.2            
2003-01-02T          1.3            
2003-01-03T          1.1            
 ...

indices are identical datetime indexes. I use the following sytnax:

df['Daily Return'].subtract(s['Return'],axis=0)

The result I get is:

ValueError: cannot reindex from a duplicate axis

What am I doing wrong?

Upvotes: 0

Views: 847

Answers (2)

Windstorm1981
Windstorm1981

Reputation: 2680

Thank you everyone.

It was actually a data issue. Data was being somehow transformed and indices in one index was corrupted on import. code was good.

I would vote myself down one if I could.

Upvotes: 0

jpp
jpp

Reputation: 164783

Your logic works fine if you've correctly defined df and s:

idx = pd.Index(['2003-01-01T', '2003-01-02T', '2003-01-03T'])

df = pd.DataFrame.from_dict({'Daily Return': [1.2, 1.3, 1.1],
                             'daily_weight': [62, 63, 64]})

s = pd.DataFrame.from_dict({'Return': [1.2, 1.3, 1.1]})

df.index, s.index = idx, idx

print(df['Daily Return'].subtract(s['Return'],axis=0))

2003-01-01T    0.0
2003-01-02T    0.0
2003-01-03T    0.0
dtype: float64

Upvotes: 1

Related Questions