0

I am not a Professional programmer at all and slowly accumulating some experience in python. This is the issue I encounter.

On my dev machine I had a python3.7 installed with pandas version 0.24.4

the following sequence was working perfectly fine.

 >>> import pandas as pd
 >>> df = pd.Series(range(3), index=pd.date_range("2000", freq="D", periods=3))
>>> df
2000-01-01    0
2000-01-02    1
2000-01-03    2
Freq: D, dtype: int64
>>> import datetime
>>> D = datetime.date(2000,1,1)
>>> df[D]
0

in the production environnent the pandas version is 1.1.4 and the sequence described does not work anymore.

>>> df[D]
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/home/ec2-user/.local/lib/python3.7/site-packages/pandas/core/series.py", line 882, in __getitem__
    return self._get_value(key)
  File "/home/ec2-user/.local/lib/python3.7/site-packages/pandas/core/series.py", line 989, in _get_value
    loc = self.index.get_loc(label)
  File "/home/ec2-user/.local/lib/python3.7/site-packages/pandas/core/indexes/datetimes.py", line 622, in get_loc
    raise KeyError(key)
KeyError: datetime.date(2000, 1, 1)

Then, unexpectedly, by transforming D in a string type the following command did work :

>>> df[str(D)]
0

Any idea of why this behaviour has changed in the different versions ? Is this behaviour a bug or will be permanent over time ? should I transform all the selections by datetime variables in the code in string variables or is there a more robust way over time to do this ?

1 Answer 1

1

It depends of version. If need more robust solution use datetimes for match DatetimeIndex:

import datetime
D = datetime.datetime(2000,1,1)

print (df[D])
0
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.