cannot import function name - python

All my files are in a same directory
I'm fresh in python and I'm trying to code functions in a Preprocessing file like this:
#Preprocessing file
from dateutil import parser
def dropOutcomeSubtype(DataFrame):
DataFrame.drop('OutcomeSubtype',axis=1,inplace='True')
def convertTimestampToTime(Serie):
for i in range(0,len(Serie)):
parser.parse(Serie[i]).time()
And then I'm trying to use it in a Exporting file like this:
#Import external librairies
import pandas as pd
import numpy as np
import re
#import our librairy
from Preprocessing import convertTimestampToTime, dropOutcomeSubtype
#Reading
Datas = pd.read_csv("../Csv/train.csv", sep=",", na_values=['NaN'])
dropOutcomeSubtype(Datas)
convertTimestampToTime(Datas.DateTime)
And when i try to run the code in my OSX shell with this config:
Python 3.5.2 |Anaconda 4.2.0 (x86_64)| IPython 5.1.0
I have get this error: cannot import name 'convertTimestampToTime'
and if change my import statement like this:
from Preprocessing import *
I get this error: name 'convertTimestampToTime' is not defined
Can you explain me why please ?
Thank you in advance

In this case you can add mod path to sys.path. if both in same dir add this code at first of main code
import os
import sys
here = os.path.abspath(os.path.dirname(__file__))
sys.path.append(here)

Related

I'm apparently having an error with win32com error, and not sure how to fix it

I'm using code to copy a df to an excel spreadsheet starting in cell A2.
Here's the code snippet. I'm including all the imports used in the code.
import pandas as pd
import numpy as np
import os
import pathlib
from pathlib import Path
import win32com.client as win32
import datetime as dt
from datetime import timedelta
import openpyxl as xl
import xlwings as xw
NewPortfolio = Path.home().joinpath("Desktop", "test.xlsx")
wb1 = Path.home().joinpath("Desktop", "test2.xlsx")
NewPortfolio_pd = pd.read_excel(NewPortfolio)
app = xw.App(visible=False)
wb = xw.Book(wb1)
ws = wb.sheets['Sheet1']
ws.range('A2').options(index=False).value = NewPortfolio_pd
wb.save()
wb.close()
app.quit()
Here's the error
AttributeError: module 'win32com.gen_py.00020813-0000-0000-C000-000000000046x0x1x9' has no attribute 'CLSIDToPackageMap'
I've already tried uninstalling and reinstalling pywin32, and have not been able to find a solution online at the moment.
If you have any comments or recommendations it'd be greatly appreciated
Thanks in advance!
Just a guess
but swap
import win32com.client as win32
with
import win32api as win32

How to import and use my own function from .py file in Python Pandas?

In Jupyter Notebook I created my own function in my_fk.py file like below:
import pandas as pd
def missing_val(df):
df= pd.DataFrame(df.dtypes, columns=["type"])
df["missing"] = pd.DataFrame(df.isna().any())
df["sum_miss"] = pd.DataFrame(df.isna().sum())
df["perc_miss"] = round((df.apply(pd.isna).mean()*100),2)
return df
Then when I try to import and run my function using below code:
import pandas as pd
import numpy as np
import my_fk as fk
df = pd.read_csv("my_data.csv")
fk.missing_val(df)
I have error like below. Error suggests that in my my_fk.py file there is no pandas as pd, but there IS line with code "import pandas as pd". How can I import and use my own function from python file ?
NameError: name 'pd' is not defined
Missing "as". Then place your pd.read_csv() after importing pandas, not before
import pandas as pd
import numpy as np
import my_fk as fk
df = pd.read_csv("my_data.csv")
fk.missing_val(df)

Extracting data from table and saving it. Layout parser package

I am new in working with python and I am using Melissa Dell's package to extract data from a table image. My image looks like this:
enter image description here
And my code, for now, is the following one:
pip install layoutparser[ocr]
import layoutparser as lp
import matplotlib.pyplot as plt
%matplotlib inline
import pandas as pd
import numpy as np
import cv2
from google.cloud.vision_v1 import types
import json
import re
from google.cloud import vision
pip show google-cloud-vision
ocr_agent = lp.GCVAgent.with_credential('mycredebtials.json',
languages = ['es'])
img = plt.imread(r'D:\pdfDispacher.do_Página_2.jpg', cv2.IMREAD_COLOR)
print(img)
plt.imshow(img)
res = ocr_agent.detect(img, return_response=True)
texts = ocr_agent.gather_text_annotations(res)
layout = ocr_agent.gather_full_text_annotation(res, agg_level=lp.GCVFeatureType.WORD)
lp.draw_box(img, layout)
lp.draw_text(img, layout, font_size=12, with_box_on_text=True,
text_box_width=1)
What I need is to tell python to get all the columns and rows and save them in CSV format. But I am not able to get this done.
I really appreciate it if anyone can help me with the next lines.

How can I read pickle file containing pandas data frame from qrc resource file with pandas read_pickle?

I have simple UI app created in PyQt5. I would like to have all of my resource files in the qrc resources.
I am using pickle data structure to store previously created DataFrame. In my app I am reading the saved pickle with pandas. When I tried to do it from the qrc_resources (created with pyrcc5) Python module I get an error.
I used same approach as in this answer:
Create a pandas dataframe from a qrc resource file
Resources file:
<!DOCTYPE RCC><RCC version="1.0">
<qresource>
<file alias="AA_data.pkl">resources/AA_data.pkl</file>
</qresource>
</RCC>
Python code:
import bisect, io
import pandas as pd
from PyQt5.QtGui import QImage
from PyQt5.QtCore import QFile, QIODevice
import qrc_resources
file = QFile(':/AA_data.pkl')
if file.open(QIODevice.ReadOnly):
f = io.BytesIO(file.readAll().data())
AA_df = pd.read_pickle(f)
Error:
ValueError: Unrecognized compression type: infer
If I do similar with Excel file it works. But with pickle file format I get an error. I am not very familiar with the data serialization and I am not able to figure it out what am I doing wrong.
You must use None for compression:
import io
import pandas as pd
from PyQt5.QtCore import QFile, QIODevice
import qrc_resources
file = QFile(':/AA_data.pkl')
if file.open(QIODevice.ReadOnly):
f = io.BytesIO(file.readAll().data())
AA_df = pd.read_pickle(f, compression=None)
print(AA_df)

UsageError: Line magic function `%cd..` not found

I get the error:
UsageError: Line magic function `%cd..` not found.
when running my python code that i usually run from Jupyter Notebook through a shell command.
I use %cd and %ls all the time in Jupiter notebooks and do not get why i can not run it from shell.
I both tried:
python test.py
and
ipython test.py
this is the relevant part of my code:
import csv
import matplotlib.pyplot as plt
from PIL import Image
import numpy as np
import pandas as pd
import sys
import os
import IPython
from scipy.misc import imread
import matplotlib.cbook as cbook
if sys.version_info[0] < 3:
from StringIO import StringIO
else:
from io import StringIO
def main():
script = sys.argv[0]
map_name = sys.argv[1]
callPenalty()
def callPenalty():
%cd standalone-penalty
os.system("octave-cli penalty.m map_bit.bmp 50 1 1 150 150")
%cd..
main()
Does anyone know how to solve that?

Categories

Resources