23

I deal with a lot of data analysis in pandas and use pandas.datetime everyday. Lately I get the warning:

"FutureWarning: The pandas.datetime class is deprecated and will be removed from pandas in a future version. Import from datetime module instead."

I've used Python datetime package before when pandas datetime wouldn't work with I wanted to do, and I understand it is a library that is more related to "date", but does anyone know the idea behind this shift to datetime package?

smci
  • 32,567
  • 20
  • 113
  • 146
tariksalay
  • 367
  • 1
  • 2
  • 9

1 Answers1

30

Summary: datetime (from the Python Standard Libary) was never really a part of Pandas or its API, which the developers are now making a concerted effort to warn about. You can access the datetime class directly:

from datetime import datetime

Detail: Older versions of Pandas (example: 0.20) contained:

from datetime import datetime

in pandas/__init__.py

This meant that, if you really wanted to, you could reference and use pd.datetime, which is really just the standard library's datetime class.

Per Pandas issue #30610 and issue #30296, pandas.datetime is really just standard datetime.datetime. It looks like deprecating them (which, interesting, is done via overriding __getattr__ at the module level*), is done to discourage "roundabout usage" where other 3rd party libraries/modules are brought into the top-level Pandas namespace.

That is, the Pandas library doesn't consider datetime (or numpy) being available in that namespace to be a part of its API, so you shouldn't depend on it being there in the first place.


*Specifically, this looks like:

# pandas/__init__.py
    def __getattr__(name):
        import warnings
        elif name == "datetime":
            warnings.warn(...)
            from datetime import datetime as dt
            return dt
Brad Solomon
  • 38,521
  • 31
  • 149
  • 235
  • 2
    So, only places where I need to make change in my past codes is (a) `from datetime import datetime` and (b) replace `pd.datetime` with `datetime` ? – Abhinav Sep 09 '20 at 23:02
  • Brad, what about with multiIndices? For example, pivot_table.index=[pandas.datetime(2012,month,day) for (month,day) in pivot_table.index] does not work anymore, if I substitute it by datetime – An old man in the sea. Dec 04 '20 at 21:42