TypeError: 'module' object is not callable despite unique function name

Question:

I am trying to clean a basic dataset with Siuba, but I am getting the following error

Traceback (most recent call last):
  File "C:Users...PycharmProjectsNTDdataanalysisvenvntdanalysis.py", line 16, in <module>
    >> select(_.primary_uza_population == _.pop)
TypeError: 'module' object is not callable

Here is the script itself:

import pandas as pd
from siuba import *
from janitor import *
from IPython.display import display

ntd_data = (
    pd.read_excel("PivotAgg.xlsm",'Sheet2', skiprows=[0,1,3,298])
    .pipe(clean_names)
    .pipe(remove_empty)
)

#print(ntd_data.columns)

(
   ntd_data
   >> select(_.primary_uza_population == _.pop)
   >> select(_.sum_of_train_revenue_miles == _.train_rev_mi)
   >> select(_.sum_of_train_miles == _.train_mi)
   >> select(_.sum_of_vehicle_passenger_car_revenue_miles == _.vehicle_rev_mi)
   >> select(_.sum_of_vehicle_passenger_car_miles == _.vehicle_mi)
)

From what i’ve found online with this error, it ocurss when the function name is confused with a class name. However, there is no class present called select.
I’ve tried using select without the >> notation as well (and without the paranthesis surrounding it) to no luck.

What is the issue here?

Asked By: josheron

||

Answers:

it ocurss when the function name is confused with a class name

Not quite. Names don’t get "confused", because a name can only ever mean one thing at a time. Assigning a name, such as select, means that it stops being a name for anything else, and starts being a name for whatever was assigned. Also, the error message says 'module' object is not callable, so the problem is that the name is being used for a module (not a class). In fact, classes are callable (that’s how you create objects), but modules are not.

Importing modules causes assignment to names. In particular, import foo means that foo becomes a name for some module object; from foo import x means that x becomes a name for something that was inside the foo module; from foo import * means that any number of names start getting used for everything the foo module specifies.

The key to understanding the problem is to notice that there isn’t any more in the stack trace. The highlighted code is select(_.primary_uza_population == _.pop); the problem is reported here; there is exactly one attempt to call anything; the thing we’re trying to call is select; the error message says there is a problem because a module is not callable; therefore, select must be a module at the time that the code runs.

janitor.functions.select is a module, which is included in the main janitor package. Therefore, from janitor import * will make select stop meaning the function from siuba, and start meaning the module from janitor instead.

This is the reason for the common advice not to use from some_package import *. That * might include many useful things, but it might also include many not useful things that actively cause a problem.

Answered By: Karl Knechtel
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.