n1k31t4
n1k31t4

Reputation: 2874

Python and importing sub-modules - Pandas example

I was trying to use the pandas.tseries.holiday module within pandas, but for some reason it was not showing up. I tried the following:

import pandas as pd

pd.tseries.<TAB>

This does give me a list of options, but holiday was among them. According to the documentation of holiday, it should be as simple as what I tried above.

This was on my system's Python. I tried it in Jupyter using Anaconda, then in Terminal and even in Emacs, but it was never found. So it must be a general design choice that I am unaware of. I have looked for clues, but all information I find tells me that importing a whole module or parts of it is a subjective choice - example: readability versus name-space pollution etc.

Eventually I just tried importing it manually (the next step would have been downloading the actual holiday file from the pandas git repository. So I did:

from pandas.tseries import holiday    # no error

holiday.<TAB>

... and I am shown all the stuff I need - great!

But what is going on here??

Looking at the actual code of holidays.py does not give me any hint as to why the file/module is not imported when I simply import pandas using the statements above.

Edit

Here is some additional information, showing how holiday is not found within pandas.tseries itself, but can be imported and used explicitly:

>>> import pandas as pd
>>> pd.tseries.holiday.USFederalHolidayCalendar()
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
AttributeError: module 'pandas.tseries' has no attribute 'holiday'
>>> from pandas.tseries import holiday
>>> holiday.USFederalHolidayCalendar()
<pandas.tseries.holiday.USFederalHolidayCalendar object at 0x7f3b18dc7fd0>

Upvotes: 3

Views: 969

Answers (1)

n1k31t4
n1k31t4

Reputation: 2874

Using simply import pandas as pd does not automatically import all sub-modules of the pandas library (as pointed out by TomAugspurger in the comments above).

This is because the __init.py__ of the pandas library does not import the everything including the holiday sub-module module.

Either adapt the __init__.py file to do so, or be aware that one must explicitly import certain sub-modules of the pandas library!

Upvotes: 2

Related Questions