Skip to content

dataframe.drop(col,axis=1) does not drop column from column.levels in multiindex dataframe #3686

Closed
@amol-desai

Description

@amol-desai

I have a multiindex dataframe from which I am dropping columns using df.drop(col,axis=1). Then, I am looking through column.levels[0] and doing some operations on all the columns. However, when I try to do this, pandas looks for the removed column since it is not removed from column.levels. Is this a bug? Is there a WAR?

Here is the df I am working with:

           ^GSPC         PF
        Adj Close  Adj Close
Date                             
2013-04-22    1562.50    4023.45
2013-04-23    1578.78    4099.20
2013-04-24    1578.79    4094.70
2013-04-25    1585.16    4124.25
2013-04-26    1582.24    4211.65
2013-04-29    1593.61    4340.75
2013-04-30    1597.57    4467.55
2013-05-01    1582.70    4432.25
2013-05-02    1597.59    4494.95
2013-05-03    1614.42    4539.55
2013-05-06    1617.50    4645.95
2013-05-07    1625.96    4624.65
2013-05-08    1632.69    4677.40
2013-05-09    1626.67    4637.25
2013-05-10    1633.70    4602.40
2013-05-13    1633.77    4618.60
2013-05-14    1650.34    4510.85
2013-05-15    1658.78    4362.00
2013-05-16    1650.47    4418.95
2013-05-17    1667.47    4406.95
2013-05-20    1666.29    4503.50
2013-05-21    1669.16    4471.20

Here is how I am dropping the columns:

    data = data.drop(stock.ticker,axis=1,level=0)

Here is where the issue is:

    print data.columns
    MultiIndex
    [(^GSPC, Adj Close), (PF, Adj Close)]

    print data.columns.labels
    [array([2, 3]), array([0, 0])]

    print data.columns.levels
    [Index([nvda, aapl, ^GSPC, PF], dtype=object), Index([Adj Close], dtype=object)]

Method used to generate DF as requested in the comment:
tickers is a list of stock ticker strings.
stock is an object that has a ticker property among others.
portfolio is an object that is a collection of stocks.

data = getdata.get_history(tickers,dt.today()-relativedelta(months=months))
data = data.drop(['Open','High','Low','Close','Volume'],axis=1)
data = data.unstack(0).swaplevel(0,1,axis=1).sortlevel(0,axis=1)
data['PF','Adj Close'] = np.zeros(len(data))
for stock in portfolio.getStocksInPortfolio():
  data['PF','Adj Close'] += data[stock.ticker,'Adj Close'] * stock.getSharesOwned()
  data = data.drop(stock.ticker,axis=1,level=0)

Metadata

Metadata

Assignees

No one assigned

    Labels

    IndexingRelated to indexing on series/frames, not to indexes themselvesMultiIndex

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions