0

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.

Nick ODell
  • 15,465
  • 3
  • 32
  • 66
ralphW
  • 1
  • 2
  • Works on my machine using your example without the datetime index. pandas version '0.17.0', python 3.5.0 – juanpa.arrivillaga Jun 17 '16 at 08:04
  • Works for me as well using 0.18.1. What version of pandas are you using? – joris Jun 17 '16 at 08:46
  • I am using Python 2.7.3 and pandas 0.11.0. – ralphW Jun 17 '16 at 11:46
  • Actually, I just wrote a separate snippet of code just to test this issue - and loc works as I would expect. The problem must be elsewhere, then :-( I do apologise for bothering you with this, and thanks for your help! – ralphW Jun 17 '16 at 12:04
  • how bizarre - I still get the same error in the full code. The dataframe df is a slice of a bigger dataframe: df = trades[trades['Security'] == "XXX"][["Trade Date","Order Type", "Quantity"]]. Cannot see a problem here, but still, loc[] behaves unexpectedly – ralphW Jun 18 '16 at 01:43

0 Answers0