Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

BUG: .drop() doesn't parse strings for DatetimeIndex. #5355

Closed
Tracked by #7
jtratner opened this issue Oct 28, 2013 · 7 comments
Closed
Tracked by #7

BUG: .drop() doesn't parse strings for DatetimeIndex. #5355

jtratner opened this issue Oct 28, 2013 · 7 comments
Labels
Datetime Datetime data dtype Dtype Conversions Unexpected or buggy dtype conversions good first issue Indexing Related to indexing on series/frames, not to indexes themselves Needs Tests Unit test(s) needed to prevent regressions

Comments

@jtratner
Copy link
Contributor

Needs to coerce:

e.g.

In [1]: import pandas.util.testing as tm

In [2]: pan = tm.makePanel()

In [3]: pan
Out[3]:
<class 'pandas.core.panel.Panel'>
Dimensions: 3 (items) x 30 (major_axis) x 4 (minor_axis)
Items axis: ItemA to ItemC
Major_axis axis: 2000-01-03 00:00:00 to 2000-02-11 00:00:00
Minor_axis axis: A to D

In [4]: pan.drop('2000-01-03', axis=1)
---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-4-11e47d7c99f1> in <module>()
----> 1 pan.drop('2000-01-03', axis=1)

../pandas/core/generic.pyc in drop(self, labels, axis, level)
   1112                 new_axis = axis.drop(labels, level=level)
   1113             else:
-> 1114                 new_axis = axis.drop(labels)
   1115             dropped = self.reindex(**{ axis_name: new_axis })
   1116             try:

../pandas/core/index.pyc in drop(self, labels)
   1614         mask = indexer == -1
   1615         if mask.any():
-> 1616             raise ValueError('labels %s not contained in axis' % labels[mask])
   1617         return self.delete(indexer)
   1618

ValueError: labels ['2000-01-03'] not contained in axis
@jreback
Copy link
Contributor

jreback commented Apr 6, 2014

related #6599

@jreback jreback modified the milestones: 0.15.0, 0.14.0 Apr 6, 2014
@jreback jreback modified the milestones: 0.16.0, Next Major Release Mar 3, 2015
@jorisvandenbossche
Copy link
Member

Updated example with frame:

In [59]: df = tm.makeTimeDataFrame()

In [60]: df
Out[60]: 
                   A         B         C         D
2000-01-03 -0.394676  0.026933  1.435611  0.735617
2000-01-04 -2.095283 -0.623525  1.017788 -2.014091
2000-01-05 -0.770375  0.643865 -0.070327 -2.305731
2000-01-06 -1.188628  0.475190 -0.926808 -0.285167
2000-01-07 -0.125856  1.719509  0.184650  0.640200
...

In [62]: df.drop('2000-01-03', axis=0)
---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-62-0844c2a2456f> in <module>()
----> 1 df.drop('2000-01-03', axis=0)

/home/joris/scipy/pandas/pandas/core/generic.py in drop(self, labels, axis, index, columns, level, inplace, errors)
   2515         for axis, labels in axes.items():
   2516             if labels is not None:
-> 2517                 obj = obj._drop_axis(labels, axis, level=level, errors=errors)
   2518 
   2519         if inplace:

/home/joris/scipy/pandas/pandas/core/generic.py in _drop_axis(self, labels, axis, level, errors)
   2547                 new_axis = axis.drop(labels, level=level, errors=errors)
   2548             else:
-> 2549                 new_axis = axis.drop(labels, errors=errors)
   2550             dropped = self.reindex(**{axis_name: new_axis})
   2551             try:

/home/joris/scipy/pandas/pandas/core/indexes/base.py in drop(self, labels, errors)
   3757             if errors != 'ignore':
   3758                 raise ValueError('labels %s not contained in axis' %
-> 3759                                  labels[mask])
   3760             indexer = indexer[~mask]
   3761         return self.delete(indexer)

ValueError: labels ['2000-01-03'] not contained in axis

In [63]: df.drop(pd.Timestamp('2000-01-03'), axis=0)
Out[63]: 
                   A         B         C         D
2000-01-04 -3.354600 -0.610585 -0.503516 -1.058210
2000-01-05  2.253061  0.083150  0.236107 -1.475713
2000-01-06 -0.299333  0.957344  2.024540 -0.424540
2000-01-07  0.396877 -1.124760 -0.249968 -1.139085
...

@jorisvandenbossche jorisvandenbossche changed the title BUG: NDFrame.drop() doesn't work with DatetimeIndex. BUG: .drop() doesn't parse strings for DatetimeIndex. Nov 24, 2017
@jreback
Copy link
Contributor

jreback commented Nov 24, 2017

@jorisvandenbossche why do you think this should work? this is partial string indexing. I mean it could, but I think drop being strict is a good thing.

@jorisvandenbossche
Copy link
Member

jorisvandenbossche commented Nov 24, 2017

I am not necessarily saying that this should work. I was going through old issues and closing/updating some, so updated the title / added a better example of the original issue here.

I agree partial strings interpretation should not work with drop, but in the example above, it is not a partial string that embodies multiple rows. It is just a single label represented by a string instead of Timestamp object. So supporting that is I think a bit less controversial.

@mroeschke mroeschke added the Bug label Mar 31, 2020
@mroeschke mroeschke removed this from the Contributions Welcome milestone Oct 13, 2022
@mroeschke
Copy link
Member

Looks to work on main now. Could use a test

@mroeschke mroeschke added good first issue Needs Tests Unit test(s) needed to prevent regressions and removed Bug labels Mar 31, 2023
PrimeF pushed a commit to PrimeF/pandas that referenced this issue Apr 18, 2023
Tests the drop function when parsing a string into DateTimeIndex
MarcoGorelli pushed a commit that referenced this issue Apr 19, 2023
* Adds test for #5355

Tests the drop function when parsing a string into DateTimeIndex

* Fix linting

---------

Co-authored-by: D067751 <fabrizio.primerano@sap.com>
@PrimeF
Copy link
Contributor

PrimeF commented Apr 19, 2023

@MarcoGorelli Can this issue be closed given that it is both fixed and tested by now?

@MarcoGorelli
Copy link
Member

thanks, not sure why it didn't close automatically

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Datetime Datetime data dtype Dtype Conversions Unexpected or buggy dtype conversions good first issue Indexing Related to indexing on series/frames, not to indexes themselves Needs Tests Unit test(s) needed to prevent regressions
Projects
None yet
Development

No branches or pull requests

6 participants