vendredi 1 juillet 2016

Dataframe loc - unexpected behaviour

I have a dataframe df which looks like this:

          Order Type  Quantity

2015-04-30        Buy       200
2015-05-06        Buy       168
2015-05-08       Sell       368
2015-06-04        Buy       126
2015-06-10        Buy       129
2015-06-17       Sell       255
2015-06-18        Buy       126
2015-06-19       Sell       126
2015-11-06        Buy       159
2016-04-01        Buy       218
2016-06-01        Buy       169

I tried to change the sign of the values in Quantity column according to Order Type value (ie keep it positive for Buy and make it negative for Sell):

df.loc[df["Order Type"] == "Sell", "Quantity"] *= -1

However, this yields something unexpected (see second and third sell values):

           Order Type  Quantity

2015-04-30        Buy       200
2015-05-06        Buy       168
2015-05-08       Sell      -368
2015-06-04        Buy       126
2015-06-10        Buy       129
2015-06-17       Sell      -368
2015-06-18        Buy       126
2015-06-19       Sell      -368
2015-11-06        Buy       159
2016-04-01        Buy       218
2016-06-01        Buy       169

I can get around the issue by adding extra column 'Sign':

df['Sign'] = 1
df.loc[df["Order Type"] == "Sell", "Sign"] = -1
df['Quantity'] *= df['Sign']

but would like to figure out the cause of the matter anyway.

Aucun commentaire:

Enregistrer un commentaire