aneroid

dropna after shift in same cell vs not, issues SettingWithCopyWarning

Feb 23rd, 2021 (edited)
196
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.46 KB | None | 0 0
  1. # for https://stackoverflow.com/q/66327949/1431750
  2.  
  3. In [1]: import pandas as pd
  4.  
  5. In [2]: pd.__version__
  6. Out[2]: '1.2.2'
  7.  
  8. In [3]: df = pd.DataFrame({
  9.    ...:     'Fruits': ['apple', 'orange', 'banana', 'pineapple', 'watermelon'],
  10.    ...:     'Volume 1': [10, 20, 30, 700, 800],
  11.    ...:     'Volume 2': [100, 200, 300, 7000, 8000],
  12.    ...:     'Volume 3': [1, 2, 3, 70, 8],
  13.    ...:     'Volume 4': [9, 8, 7, 6, 5],
  14.    ...:     'Other': [-1, -2, -3, -4, -5]
  15.    ...: })
  16.    ...: df.loc[:, 'Volume 2':'Volume 4'] = df.loc[:, 'Volume 2':'Volume 4'].shift(1)
  17.    ...: df
  18. Out[3]:
  19.        Fruits  Volume 1  Volume 2  Volume 3  Volume 4  Other
  20. 0       apple        10       NaN       NaN       NaN     -1
  21. 1      orange        20     100.0       1.0       9.0     -2
  22. 2      banana        30     200.0       2.0       8.0     -3
  23. 3   pineapple       700     300.0       3.0       7.0     -4
  24. 4  watermelon       800    7000.0      70.0       6.0     -5
  25.  
  26. In [4]: df = df.dropna()
  27.    ...: df
  28. Out[4]:
  29.        Fruits  Volume 1  Volume 2  Volume 3  Volume 4  Other
  30. 1      orange        20     100.0       1.0       9.0     -2
  31. 2      banana        30     200.0       2.0       8.0     -3
  32. 3   pineapple       700     300.0       3.0       7.0     -4
  33. 4  watermelon       800    7000.0      70.0       6.0     -5
  34.  
  35. In [5]: # will issue warning
  36.    ...: df.loc[:, 'Volume 2':'Volume 4'] = df.loc[:, 'Volume 2':'Volume 4'].astype(int)
  37.    ...: df
  38. c:\program files\python38\lib\site-packages\pandas\core\indexing.py:1787: SettingWithCopyWarning:
  39. A value is trying to be set on a copy of a slice from a DataFrame.
  40. Try using .loc[row_indexer,col_indexer] = value instead
  41.  
  42. See the caveats in the documentation: https://pandas.pydata.org/pandas-docs/stable/user_guide/indexing.html#returning-a-view-versus-a-copy
  43.   self._setitem_single_column(loc, val, pi)
  44. Out[5]:
  45.        Fruits  Volume 1  Volume 2  Volume 3  Volume 4  Other
  46. 1      orange        20       100         1         9     -2
  47. 2      banana        30       200         2         8     -3
  48. 3   pineapple       700       300         3         7     -4
  49. 4  watermelon       800      7000        70         6     -5
  50.  
  51. In [6]: # let's start over but this time, `dropna` after `shift` **in the same cell**:
  52.    ...:
  53.    ...: df = pd.DataFrame({
  54.    ...:     'Fruits': ['apple', 'orange', 'banana', 'pineapple', 'watermelon'],
  55.    ...:     'Volume 1': [10, 20, 30, 700, 800],
  56.    ...:     'Volume 2': [100, 200, 300, 7000, 8000],
  57.    ...:     'Volume 3': [1, 2, 3, 70, 8],
  58.    ...:     'Volume 4': [9, 8, 7, 6, 5],
  59.    ...:     'Other': [-1, -2, -3, -4, -5]
  60.    ...: })
  61.    ...: df.loc[:, 'Volume 2':'Volume 4'] = df.loc[:, 'Volume 2':'Volume 4'].shift(1)
  62.    ...: df = df.dropna()
  63.    ...: df
  64. Out[6]:
  65.        Fruits  Volume 1  Volume 2  Volume 3  Volume 4  Other
  66. 1      orange        20     100.0       1.0       9.0     -2
  67. 2      banana        30     200.0       2.0       8.0     -3
  68. 3   pineapple       700     300.0       3.0       7.0     -4
  69. 4  watermelon       800    7000.0      70.0       6.0     -5
  70.  
  71. In [7]: # NO WARNINGS next...
  72.    ...: df.loc[:, 'Volume 2':'Volume 4'] = df.loc[:, 'Volume 2':'Volume 4'].astype(int)
  73.    ...: df
  74. Out[7]:
  75.        Fruits  Volume 1  Volume 2  Volume 3  Volume 4  Other
  76. 1      orange        20       100         1         9     -2
  77. 2      banana        30       200         2         8     -3
  78. 3   pineapple       700       300         3         7     -4
  79. 4  watermelon       800      7000        70         6     -5
  80.  
  81. In [8]:
  82.  
Add Comment
Please, Sign In to add comment