1

My code started getting cluttered with functions so I wanted to make a library(or module I guess) of all my functions stored in it. Most functions that work in my code give the following error when importing them from a module:

takes 1 positional argument but 2 were given

I created a file called mylib.py.

The first function I placed in it, and of many that didn't work was this one:

def load(hist_indicator, data):   
    mask1 = data['IndicatorName'].str.contains(hist_indicator)
    dfdata = data[mask1]

    filter = dfdata['Year'] > 1995
    df = dfdata[filter]
    return df

All it does is it takes a pandas DataFrame and filters out data I need. The function works if it is in the code itself. I used it like this:

ind_internet = 'Internet users \(per 1'
internet = load(ind_internet, data)

However, when I tried using it from mylib.py I got the aforementioned error. Tried using it like this:

import mylib
ind_internet = 'Internet users \(per 1'
internet = mylib.load(ind_internet, data)

I hope you can let me know what I'm doing wrong.

Edit:

To add to the question, here's the code with a little example:

import pandas as pd

def load(hist_indicator, data):   
    mask1 = data['IndicatorName'].str.contains(hist_indicator)
    dfdata = data[mask1]

    filter = dfdata['Year'] > 1995
    df = dfdata[filter]
    return df

dataf = {'CountryName':  ['France', 'Germany'],
        'CountryCode': ['FRA', 'GER'],
        'IndicatorName': ['Internet users (per 100)', 'GDP per capita (current US$)'],
        'IndicatorCode' : ['MS.MIL.XPRT.KD','SP.POP.DPND.YG'],
        'Year' : [2000,1983],
        'Value' : [15.3,4322.27]
        }

data = pd.DataFrame (dataf, columns = ['CountryName','CountryCode','IndicatorName','IndicatorCode','Year','Value'])

ind_internet = 'Internet users \(per 1'
internet = load(ind_internet, data)
internet

Also, an edit: I didn't actually type import mylib.py in the code, only here by mistake.

Learnos
  • 11
  • 2
  • 1
    Possible duplicate of [Import Python Script Into Another?](https://stackoverflow.com/questions/15696461/import-python-script-into-another) – m13op22 Aug 14 '19 at 22:03
  • 3
    You need to provide a [mcve]. Your code as posted would throw other errors, and there's no reason for it to throw the error you've shown. – juanpa.arrivillaga Aug 14 '19 at 23:02

1 Answers1

0

The problem:
The import line import mylib.py is incorrect. You do not need the file extension .py

A solution
Setup for package like this:

package
   |-mylib.py
   |-test_import_mylip.py

Then in mylib.py,

# package/mylib.py
def load(hist_indicator, data):   
    mask1 = data['IndicatorName'].str.contains(hist_indicator)
    dfdata = data[mask1]

    filter = dfdata['Year'] > 1995
    df = dfdata[filter]
    return df

Then to import and use mylib.load from test_import_mylib.py,

# test_import_mylib.py
import mylib
import pandas as pd

data = pd.read_csv('path/to/df.csv')
ind_internet = 'Internet users \(per 1'

internet = mylib.load(ind_internet, data)
SkippyElvis
  • 100
  • 6
  • this has nothing to do with classes vs functions. The import system does not distinguish between them, they are all merely attributes in a module. In any case, the op already tried this, and they wouldn't be getting the errors you mention. – juanpa.arrivillaga Aug 14 '19 at 23:00