Python pandas apply function defined in class - python

I am currently using Spyder IDE. I am trying to apply a self-compiled function sfill to my dataframe based on the different groups. Whenever I create this function, I get the following error:
TypeError: sfill() takes exactly 1 argument (2 given)
I am certain the function sfill() works correctly, when run standalone. What am I doing wrong?
How do I allow Python to show the dataframe want without using statement global want in the variable explorer? How do I use return in this instant?
My code is as follows:
import pandas as pd
have = pd.DataFrame({ \
"groups": pd.Series(["group1","group1","group1","group2","group2","group2"]) \
,"a0": pd.Series(["abc","1","something here","abc2","1","something here"]) \
,"a1": pd.Series(["","2","something here","","","something here"]) \
,"a2": pd.Series(["","3","something here","","3","something here"]) \
,"a3": pd.Series(["something","1","something here","something","1","something here"]) \
,"a4": pd.Series(["","2","something here","","2","something here"]) \
,"a5": pd.Series(["","","something here","","","something here"]) \
,"a6": pd.Series(["","","something here","","","something here"]) \
,"a7": pd.Series(["cdf","5","something here","mnop","5","something here"]) \
,"a8": pd.Series(["","6","something here","","6","something here"]) \
,"a9": pd.Series(["xyz","1","something here","xyz","1","something here"]) \
})
class main(object):
def sfill(vector):
vector = vector.copy()
vector.i0, vector.i1 = vector.index[0], vector.index[1]
vector.cond = have.loc[vector.i1].ne('')
vector.loc[vector.i0, vector.cond] = vector.loc[vector.i0, vector.cond].str.strip().replace('', None)
return vector
def Development_plan(selfdev):
global want
want=have.groupby('groups', group_keys=False, sort=False).apply(selfdev.sfill)
a=main()
a.Development_plan() # this is where the issue exists
Thanks.

functions in a class should take self as first argument
def sfill(self, vector):
...
when you call selfdev.sfill(soemthing) it actually calls sfill(selfdev, something)
self is only a naming convention, in your Development_plan() you use selfdev for this.

Related

Python Dreambooth Google Colab, help passing int args to !accelerate launch train_dreambooth.py

I am trying to add a markup param for the seed but I keep getting this error
--seed: invalid int value: ''
I have other markup paramaters being sent as args only integers don't seem to work.
Here is the code I used to make the variable
AiSeed = 420 ##param {type:"number"}
I've also tried
AiSeed = 420 ##param {type:"integer"}
Here is my config, I'm expecting other Integers not to work as well with the current method
!accelerate launch train_dreambooth.py \
--pretrained_model_name_or_path=$TrainingModelName \
--pretrained_vae_name_or_path="stabilityai/sd-vae-ft-mse" \
--instance_data_dir=$INSTANCE_DIR \
--class_data_dir=$CLASS_DIR \
--output_dir=$OUTPUT_DIR \
--with_prior_preservation --prior_loss_weight=1.0 \
--instance_prompt=$Prompt \
--class_prompt=$CLASS_NAME \
--seed=$AiSeed \
--resolution=$ImgResolution \
--train_batch_size=1 \
--train_text_encoder \
--mixed_precision="fp16" \
--use_8bit_adam \
--gradient_accumulation_steps=1 \
--learning_rate=1e-6 \
--lr_scheduler="constant" \
--lr_warmup_steps=0 \
--num_class_images=$ClassImgCount \
--sample_batch_size=4 \
--max_train_steps=$MaxSteps \
--save_interval=$SaveInterval \
--gradient_checkpointing
Does anyone know the proper way to pass int arguments to accelerate?
I've also tried
f"{AiSeed}"
"{AiSeed}"

Python calling a function from different module using a variable defined in current module

There is a module in helpers directory called helper_a.py.
It has all classes and functions defined here.
Now I want to call a function from here into another module (not in helpers directory) but one step (cd ..) behind. (init.py) is in helpers directory.
Code and error is as below :
from helpers.helper_a import *
import pandas as pd
query_train_data = "select * from train;"
df_train_dataset = pd.read_sql(query_train_data, con=engDps1000)
query_test_data = "select * from test;"
df_test_dataset = pd.read_sql(query_test_data, con=engDps1000)
df_train_data = df_train_dataset
df_test_data = df_test_dataset
data_prep_steps() # This function is defined in helpers_a
Error:
---------------------------------------------------------------------------
NameError Traceback (most recent call last)
<ipython-input-12-3c88b46f341a> in <module>
----> 1 data_prep_steps()
~\Desktop\Notebooks\helpers\helper_a.py in data_prep_steps()
---> 89 # STEP 1 : CONVERT REQUIRED COLS TO CATEGORIES
90 for df_name in [df_train_data, df_test_data]:
91 data_prep_class = data_prep(df_name)
NameError: name 'df_train_data' is not defined
Question is that the variable df_train data is defined in the current module and i want to use it in the function defined in helpers_a by calling it also in the current module, but why is it not recognizing this variable??
Note : Also tried assigning global variable status but it still doesnt solve the issue
There is a solution to create non existing attributes,methods or functions in other modules. It comes from unit testing.
from unittest.mock import patch, PropertyMock
from helpers.helper_a import *
import pandas as pd
query_train_data = "select * from train;"
df_train_dataset = pd.read_sql(query_train_data, con=engDps1000)
query_test_data = "select * from test;"
df_test_dataset = pd.read_sql(query_test_data, con=engDps1000)
df_train_data = df_train_dataset
df_test_data = df_test_dataset
with patch('helpers.helper_a.df_test_data',create=True,new_callable=PropertyMock) as df_test_data_mock: #Create tells to create attribute if it does not exist
with patch('helpers.helper_a.df_train_data', create=True, new_callable=PropertyMock) as df_train_data_mock: # PropertyMock is used to mock properties
df_test_data_mock.return_value = df_test_data
df_train_data_mock.return_value = df_train_data
data_prep_steps() # This function is defined in helpers_a
Although I agree with comments that passing those values would be way simpler. Also due to python dynamic nature you can just simply set those attributes on the module itself. This method is way simpler but you need to remember to clean up after your done which previous method does for you with context mananger.
import helpers.helper_a
import pandas as pd
query_train_data = "select * from train;"
df_train_dataset = pd.read_sql(query_train_data, con=engDps1000)
query_test_data = "select * from test;"
df_test_dataset = pd.read_sql(query_test_data, con=engDps1000)
helpers.helper_a.df_train_data = df_train_dataset
helpers.helper_a.df_test_data = df_test_dataset
helpers.helper_a.data_prep_steps() # This function is defined in helpers_a

VIM command to insert multiline text with argument

new VIM user. I'm trying to make creating python properties easier for my class definitions. What I would like for say I type
:pyp x
then VIM will autofill where my cursor is
#property
def x(self):
return self.x
#property.setter
def x(self,val):
self._x = val
or more abstractly I type
:pyp <property_name>
and VIM fills
#property
def <property_name>(self):
return self.<property_name>
#property.setter
def <property_name>(self,val):
self._<property_name> = val
I've looked at a few posts and the wikis on functions, macros but I'm very unsure of how to go about it or what to even look up as I am brand new VIM user, less than a week old.
I tried using [this][1] as an example, in my .vimrc but I couldn't even get that to work.
Edit:
So the code I am currently trying is
function! PythonProperty(prop_name)
let cur_line = line('.')
let num_spaces = indent('.')
let spaces = repeat(' ',num_spaces)
let lines = [ spaces."#property",
\ spaces."def ".prop_name."(self):",
\ spaces." return self.".property,
\ spaces."#property.setter",
\ spaces."def".prop_name."(self,val)",
\ spaces." self._".prop_name." = val" ]
call append(cur_line,lines)
endfunction
and I am getting the errors
E121: Undefined variable: prop_name
I am typing
`:call PythonProperty("x")`
[1]: https://vi.stackexchange.com/questions/9644/how-to-use-a-variable-in-the-expression-of-a-normal-command
E121: Undefined variable: prop_name
In VimScript variables have scopes. The scope for function arguments is a:, while the default inside a function is l: (local variable). So the error means that l:prop_name was not yet defined.
Now how I do this:
function! s:insert_pyp(property)
let l:indent = repeat(' ', indent('.'))
let l:text = [
\ '#property',
\ 'def <TMPL>(self):',
\ ' return self.<TMPL>',
\ '#property.setter',
\ ' def <TMPL>(self,val):',
\ ' self._<TMPL> = val'
\ ]
call map(l:text, {k, v -> l:indent . substitute(v, '\C<TMPL>', a:property, 'g')})
call append('.', l:text)
endfunction
command! -nargs=1 Pyp :call <SID>insert_pyp(<q-args>)
Alternatively, we can simulate actual key presses (note that we don't need to put indents in the template anymore; hopefully, the current buffer has set ft=python):
function! s:insert_pyp2(property)
let l:text = [
\ '#property',
\ 'def <TMPL>(self):',
\ 'return self.<TMPL>',
\ '#property.setter',
\ 'def <TMPL>(self,val):',
\ 'self._<TMPL> = val'
\ ]
execute "normal! o" . substitute(join(l:text, "\n"), '\C<TMPL>', a:property, 'g') . "\<Esc>"
endfunction
command! -nargs=1 Pyp2 :call <SID>insert_pyp2(<q-args>)
its very very difficult if not impossible to get pluggins
I suggest you to watch this video on youtube. In fact, many of Vim plugins are just overkill.

How to use a string for icon bitmap?

Is there a way to use a string for the iconbitmap in the Tkinter (Python 2.7.9) module?
I know that you can prodive a file path (even though I haven't understood where the difference between default and bitmap as parameters is.
The reason I am asking is because I want to create out of a Python script an .exe with py2exe (which works), but I would need to create a icon file then to be able to use an icon.
Any workaround or other method is appreciated.
(Note to folks using Python 3, see my supplemental answer for an alternative that only works in that version.)
I don't know of any way to pass iconbitmap() anything other than a file path in Python 2.x, so here's a workaround that creates a temporary file from a string representation of icon file's contents to pass it. It also shows a way to ensure the temporary file gets deleted.
import atexit
import binascii
import os
import tempfile
try:
import Tkinter as tk
except ModuleNotFoundError: # Python 3
import tkinter as tk
iconhexdata = '00000100010010100000010018006803000016000000280000001000000020' \
'000000010018000000000040030000130b0000130b00000000000000000000' \
'ffffff6c6c6d6c6c6d6c6c6d6c6c6d6c6c6d6c6c6d6c6c6d6c6c6d6c6c6d6c' \
'6c6d6c6c6d6c6c6d6c6c6d6c6c6dffffffffffff6c6c6d6c6c6d6c6c6d6c6c' \
'6d6c6c6d6c6c6d6c6c6d6c6c6d6c6c6d6c6c6d6c6c6d6c6c6d6c6c6d6c6c6d' \
'ffffffffffff6c6c6d6c6c6dffffffffffffffffffffffffffffffffffffff' \
'ffffffffffffffffffffff6c6c6d6c6c6dffffffffffff6c6c6d6c6c6dffff' \
'ff6c6c6d6c6c6d6c6c6d6c6c6d6c6c6d6c6c6d6c6c6d6c6c6dffffff6c6c6d' \
'6c6c6dffffffffffff6c6c6d6c6c6dffffff6c6c6d6c6c6d6c6c6d6c6c6d6c' \
'6c6d6c6c6d6c6c6d6c6c6dffffff6c6c6d6c6c6dffffffffffff6c6c6d6c6c' \
'6dfffffffffffffffffffffffffffffff2f4f7d6dfe9b8cadb95b2cfedf2f6' \
'6c6c6d6c6c6dfffffffffffffffffffffffff4f7fac0d4e69bb9d6739dc657' \
'89ba3e78b03f78af4177ad4276abd2deeaffffffffffffffffffffffffffff' \
'ffffffffdfe9f24178ad4178ad4178ad5081b17398be9db8d3bed4e6bbd7ec' \
'add7f3fffffffffffffffffffffffffffffffffffff8fafcaac2dac4d3e4df' \
'e8f1f9fbfdfffffff4fafd91cff520a3f10297eee4f4feffffffffffffffff' \
'ffffffffffffffffffffffffffffffffffffffe7f4fd7fcaf6159def0595ec' \
'179fec82c7f4bad6f7fdfefffffffffffffffffffffffffffffffffdfeffdb' \
'f0fd7bc8f6119bed0695eb1a9ded7ecaf5f0f8febfd3f73165e495b1f1ffff' \
'fffffffffffffffffffffffffff6fbfe2fa6ee0695eb1b9eed86ccf5e8f6fd' \
'ffffffd2dff93468e5326ae5c7d6f8ffffffffffffffffffffffffffffffff' \
'ffff96d2f784cbf5eaf6fdffffffffffffe3eafb4275e72c66e4b6caf6ffff' \
'ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff' \
'f3f6fd5784ea2c66e499b5f2ffffffffffffffffffffffffffffffffffffff' \
'fffffffffffffffffffffffffffffdfeff7097ed2c66e47a9eeeffffffffff' \
'fffffffffffffffffffffffffffffffffffffffffffffffffffffffffdfeff' \
'85a7ef2c66e4608cebf9fbfeffffffffffffffffffffffff00000000000000' \
'00000000000000000000000000000000000000000000000000000000000000' \
'0000000000000000000000000000000000000000000000000000'
def on_closing(iconfile):
try:
os.remove(iconfile.name)
except Exception:
pass
with tempfile.NamedTemporaryFile(delete=False) as iconfile:
iconfile.write(binascii.a2b_hex(iconhexdata))
# Register a clean-up function.
atexit.register(lambda file=iconfile: on_closing(file))
root = tk.Tk()
root.title('stackoverflow!')
root.iconbitmap(iconfile.name)
tk.Label(root, text='Note the custom icon').pack()
tk.Button(root, text='OK', bg='lightgreen', command=root.quit).pack()
root.mainloop()
The window displayed will have a custom icon as shown below:
You didn't ask how to do it, but here's the code I used to convert the original .ico file into the Python string variable used in my example:
from __future__ import print_function
import binascii
try:
from itertools import izip_longest as zip_longest
except ImportError:
from itertools import zip_longest
iconfile = 'stackoverflow.ico' # Path to icon file.
VAR_NAME = 'iconhexdata'
VAR_SUFFIX = ' = '
INDENTATION = ' ' * len(VAR_NAME+VAR_SUFFIX)
MAX_LINE_LENGTH = 80
EXTRA_CHARS = '"" \\' # That get added to each group of hex digits.
LINE_LENGTH = MAX_LINE_LENGTH - len(INDENTATION) - len(EXTRA_CHARS)
def grouper(chunk_size, iterable):
""" Collect data into fixed-length chunks or blocks.
s -> (s0,s1,...sn-1), (sn,sn+1,...s2n-1), (s2n,s2n+1,...s3n-1), ...
"""
return zip_longest(*[iter(iterable)]*chunk_size, fillvalue='')
with open(iconfile, 'rb') as imgfile:
hexstr = [chr(x) for x in bytearray(binascii.b2a_hex(imgfile.read()))]
hexlines = (''.join(str(x) for x in group) for group in grouper(LINE_LENGTH, hexstr))
print(VAR_NAME + VAR_SUFFIX, end='')
print((' \\\n' + INDENTATION).join((repr(line) for line in hexlines)))
This doesn't answer your Python 2.7 question, but may be of interest to others nowadays who are using Python 3. In the later version of Python the tkinter module has an alternative to the iconbitmap() function—named iconphoto()—that can be used to set the title-bar icon of any tkinter/toplevel window. However, unlike the former method, the image should be the instance of the tkinter.PhotoImage class, not only a file path string — and there are ways to create a PhotoImage object from a byte string in the program so there would be no need to use a file at all, even a temporary one as was the case in my original answer.
#!/usr/bin/env python3
iconimgdata = b'iVBORw0KGgoAAAANSUhEUgAAABAAAAAQCAIAAACQkWg2AAAAGXRFWHRTb2Z0d' \
b'2FyZQBBZG9iZSBJbWFnZVJlYWR5ccllPAAAAXpJREFUeNpi+I8V/Pv7fnnrkz' \
b'Sd1z0J/37/RJZhYsAKGJkEwis4DV1+3jrzYXEjsgwODRA98c1sctrfTmz6vG0' \
b'WQhzNLd8vTft6uuXvt1cQ7u9Xj5+XOgHd9u3UNogIioZ/v7+9W6b/eirb23nS' \
b'X8+0/f32Aij48/6lpxkmT7OMflw7ju4HRmZ2fq813MalDH+/fTvZ8GG50bfT9' \
b'aySckLZ3f9///95+xxIDcgWDPDv64uf12b8vDzt748PDFxCHBrZzPwWHBrOQI' \
b'8hNPz9/fPeiU1cglK8otI8wlJMLGz/fn/9cXXunyv9f788Eoh9xMgtDVTGAjf' \
b'12/vnl7dNh7BZOPl5xZVFFbSEZXTZTGazM3yCqEZx0u8fX9/cPfPh6e0PT258' \
b'efMEqP8/A+O//0z//jPaZ0wQVdRFaMjLzQWyJk2ejOyNH18/f3r95NPrR19e3' \
b'FV3iCivqoeoYUFWBNGJCSb5ChER0zgAig1oriKgAZd70ADJTgIIMACVtvtL6F' \
b'X2cAAAAABJRU5ErkJggg=='
import base64
import tkinter as tk # Python 3
root = tk.Tk()
root.title('stackoverflow!')
root.geometry('225x50')
img = base64.b64decode(iconimgdata)
photo = tk.PhotoImage(data=img)
root.iconphoto(False, photo)
tk.Label(root, text='Note the custom icon').pack()
tk.Button(root, text='OK', bg='lightgreen', command=root.quit).pack()
root.mainloop()
Here's what the demo looks like running:
Here again is the code I used to convert the 16x16 pixel .png image file I had into the Python bytestring variable used in the code in my example above. Here's a link to the small stackoverflow.png image file being used.
#!/usr/bin/env python3
import base64
from itertools import zip_longest
imgfilepath = 'stackoverflow.png' # Path to an image file.
VAR_NAME = 'iconimgdata'
VAR_SUFFIX = ' = '
INDENTATION = ' ' * len(VAR_NAME+VAR_SUFFIX)
MAX_LINE_LENGTH = 79
EXTRA_CHARS = '"" \\' # That get added to each group of hex digits.
LINE_LENGTH = MAX_LINE_LENGTH - len(INDENTATION) - len(EXTRA_CHARS)
def grouper(chunk_size, iterable):
""" Collect data into fixed-length chunks or blocks.
s -> (s0,s1,...sn-1), (sn,sn+1,...s2n-1), (s2n,s2n+1,...s3n-1), ...
"""
return zip_longest(*[iter(iterable)]*chunk_size, fillvalue='')
with open(imgfilepath, 'rb') as file:
hexstr = [chr(x) for x in base64.b64encode(file.read())]
hexlines = (''.join(str(x) for x in group) for group in grouper(LINE_LENGTH, hexstr))
print(VAR_NAME + VAR_SUFFIX + 'b', end='')
print((' \\\n' + INDENTATION + 'b').join((repr(line) for line in hexlines)))

TypeError ( 'module' object is not callable )

I have two Scripts. Script 1 is titled schemeDetails.The second script is a test script called temporaryFile that creates a schemeSetup object using the schemeSetup class which is within schemeDetails. Everything is hunky dory up to the point where I try to acess the method insertScheme which is within the schemeSetup Class.
I have imported the schemeDetails script using the following:
import schemeDetails
reload(schemeDetails)
from schemeDetails import *
I can create the schemeDetails Object and access its attributes
d = schemeDetails.schemeSetup() -- fine
print(d.scheme) -- fine
d.insertScheme() -- throws error
but trying to call the insertScheme function throws an error
I don't know why this is happening as the import statement looks above board to me. Any advice appreciated
from sikuli import *
import os
class schemeSetup(object):
#Uses default values
def __init__(
self,
scheme = "GM",
cardNumber = "1234567A",
month = "December",
year = "2015",
setSchemeAsDefault = True):
#Provide default values for parameters
self.scheme = scheme
self.cardNumber = cardNumber
self.month = month
self.year = year
self.setSchemeAsDefault = setSchemeAsDefault
#schemeDetails is not a sub
# class of patient. It is simply defined within the patient class
# - there is a huge difference.
#====================================================#
#schemeDetails Function
def insertScheme(self):
print("insertScheme Works")
#r = Regions()
#r.description("Patient Maintenance", "schemeDetails")
#myRegion = r.createRegion()
#myRegion.highlight(1)
#click(myRegion.find(insertSchemeButton))
#click(myRegion.find(blankSchemeEntry))
#type(self.scheme + Key.ENTER + Key.ENTER)
#type(self.cardNumber + Key.ENTER)
#type(self.month + Key.ENTER)
#type(self.year + Key.ENTER)
#type(" ")
#unticks HT link, HT linking should be in a separate function
#====================================================#
#schemeDetails Function
def editScheme(self):
print("editScheme Works")
#====================================================#
def deleteScheme(self):
pass
#====================================================#
It may be of importance that calling either of the bottom functions does not produce an error. If I put print("Hello") under editScheme, and call that method using s.editScheme the program compiles but I get no output. If I run print(s.editScheme) it returns None
Well it seems to be fixed now after changing the import format to this
import schemeDetails
from schemeDetails import schemeSetup
s = schemeDetails.schemeSetup()

Categories