Pass column name to function - python-3.x

I am trying to create a function in which i want to pass pandas dataframe's column name.
Basically i am using plotly.express and that is what i am embedding into a function.
But when i am doing so, it is saying that plotly requires column name but no colomn name found.
def bar(df,**kwargs):
val=", ".join(f"{key}={value}" for key, value in kwargs.items())
px.bar(df,val).show()
bar(sales,x='Category',y='Quantity')```
How can i solve this.
So that whenever user passes an argument it consider it as a field not just a string.
Any leads will be helpful.
TIA

This is not really a plotly question, but python. Fundamentals of how argument passing works.
Simply you just need to pass the kwargs pass to your function to px.bar()
import plotly.express as px
import pandas as pd
import numpy as np
def bar(df,**kwargs):
px.bar(df,**kwargs).show()
sales = pd.DataFrame({"Category":np.random.choice(list("ABCD"), 20), "Quantity":np.random.randint(1,10,20)})
bar(sales,x='Category',y='Quantity')

Related

How to add a custom feature calculator to the tsfresh.feature_extraction.feature_calculators submodule?

import pandas as pd,numpy as np
import tsfresh
from tsfresh.feature_extraction.feature_calculators import set_property
#set_property("fctype", "simple")
def zero_value_percentage(x):
ratio = (x==0).sum()/x.shape[0]
return ratio`
fc_parameters = {'mean':None, 'standard_deviation':None, 'zero_value_percentage':None}
df_features = tsfresh.extract_features(data[['id','year_month','order_qty']], column_id='id', column_sort='year_month', default_fc_parameters=fc_parameters)
Error: 'module 'tsfresh.feature_extraction.feature_calculators' has no attribute 'zero_value_percentage''
Utility of custom function is to count no. of non zero points in timeseries as a %.
The documentation mentions to "add the feature calculator to the tsfresh.feature_extraction.feature_calculators submodule" at the end of Step 2 in the link for it to work.
How does one do that?
Dont know how to proceed

'int' object has no attribute 'iloc' Viewing data Python/Pandas error

I am trying to view the imported data through pandas, however when I run the script I am presented with the error "int' object has no attribute 'iloc". I am new here and this is my first post so apologies if I don't know the rules. My code is below.
import pandas as pd
from matplotlib import pyplot
from pandas import read_csv
filename = '/Users/rahulparmeshwar/Documents/Algo Bots/Data/Live Data/Tester.csv'
data = read_csv(filename)
pd.set_option('display.width',100)
pd.DataFrame.head(1)
print(pd)
I am new to Python too.
You want to call head() on your dataframe data, not on the constructor pd.DataFrame.
data = read_csv(filename)
pd.set_option('display.width',100)
print(data.head(1))

what attributes can I set in Matplotlib's function ax.set()

From the Matplotlib's official website I can get that the function 'ax.set()' can bset attributes.But I haven't known what attributes can I set actually.For instance:
from matplotlib import pyplot as plt
import numpy as np
import matplotlib
fig=plt.figure(
figsize=(15,6),
dpi=120,
)
#definition layout
layout=(2,4)
#create
axes=fig.subplots(*layout)
ax_plot_function=axes[0][0]
ax_scatter=axes[0][1]
#plot the first axes
props = {
'title': 'function_example$f(x)=cos(x)$',
'xlabel': 'X',
'ylabel':'Y',
'xlim':(-(2*np.pi),2*np.pi),
'ylim':(-(np.pi),np.pi),
'grid':True,
}
ax_plot_function.set(**props)
x=np.arange(-(2*np.pi),2*np.pi,0.01)
y=np.cos(x)
ax_plot_function.plot(x,y)
plt.show()
in Line29 happens an error that says
AttributeError: 'AxesSubplot' object has no property 'grid'
So I know that the function 'set()' can not have the attribute 'grid' in Line27,But how can I fix it and What attributes can I set exactly by using the function 'ax.set()'.
Could I get a list of all attributes that can set by the function 'set()'? Or Please tell me where I can find the list.
ax.set() can set the properties defined in class matplotlib.axes.Axes.
There is also set_xxx() api where xxx is also the property in class matplotlib.axes.Axes.
The idea is simple, image you create a Python class and some properties. The related getter and setter method should be defined. Also, the constructor method can take initial values of those properties.
If you want to turn on the grid, use ax.grid(True).

Sphinx autodoc does not display all types or circular import error

I am trying to auto document types with sphinx autodoc, napoleon and autodoc_typehints but I am having problems as it does not work with most of my types. I am using the deap package to do some genetic optimization algorithm, which makes that I have some very specific types I guess sphinx cannot handle.
My conf.py file looks like this:
import os
import sys
sys.path.insert(0, os.path.abspath('../python'))
extensions = [
'sphinx.ext.autodoc',
'sphinx.ext.viewcode',
'sphinx.ext.napoleon',
'sphinx_autodoc_typehints'
]
set_type_checking_flag = False
always_document_param_types = False
I have an Algo.rst file with:
.. automodule:: python.algo.algo
:members: crossover_worker,
test
and my python.algo.algo module looks like this (I've added a dummy test function to show it works whenever I have no special types specified):
# Type hinting imports
from config.config import Config
from typing import List, Set, Dict, NamedTuple, Union, Tuple
from types import ModuleType
from numpy import ndarray
from numpy import float64
from multiprocessing.pool import MapResult
from deap.tools.support import Logbook, ParetoFront
from deap.base import Toolbox
from pandas.core.frame import DataFrame
from deap import creator
...
def crossover_worker(sindices: List[creator.Individual, creator.Individual]) -> Tuple[creator.Individual, creator.Individual]:
"""
Uniform crossover using fixed threshold
Args:
sindices: list of two individuals on which we want to perform crossover
Returns:
tuple of the two individuals with crossover applied
"""
ind1, ind2 = sindices
size = len(ind1)
for i in range(size):
if random.random() < 0.4:
ind1[i], ind2[i] = ind2[i], ind1[i]
return ind1, ind2
def test(a: DataFrame, b: List[int]) -> float:
"""
test funcition
Args:
a: something
b: something
Returns:
something
"""
return b
When settings in conf.py are like above I have no error, types for my test function are correct, but types for my crossover_worker function are missing:
However, when I set the set_type_checking_flag= True to force using all types, I have a circular import error:
reading sources... [100%] index
WARNING: autodoc: failed to import module 'algo' from module 'python.algo'; the following exception was raised:
cannot import name 'ArrayLike' from partially initialized module 'pandas._typing' (most likely due to a circular import) (/usr/local/lib/python3.8/site-packages/pandas/_typing.py)
looking for now-outdated files... none found
And I never import ArrayLike so I don't get it from where it comes or how to solve it?
Or how to force to import also the creator.Individual types that appear everywhere in my code?
My sphinx versions:
sphinx==3.0.1
sphinx-autodoc-typehints==1.10.3
After some searching there were some flaws with my approach:
Firstly a "list is a homogeneous structure containing values of one type. As such, List only takes a single type, and every element of that list has to have that type." (source). Consequently, I cannot do something like List[creator.Individual, creator.Individual], but should transform it to List[creator.Individual] or if you have multiple types in the list, you should use an union operator, such as List[Union[int,float]]
Secondly, the type creator.Individual is not recognized by sphinx as a valid type. Instead I should define it using TypeVar as such:
from typing import TypeVar, List
CreatorIndividual = TypeVar("CreatorIndividual", bound=List[int])
So by transforming my crossover_worker function to this, it all worked:
def crossover_worker(sindices: List[CreatorIndividual]) -> Tuple[CreatorIndividual, CreatorIndividual]:
Note: "By contrast, a tuple is an example of a product type, a type consisting of a fixed set of types, and whose values are a collection of values, one from each type in the product type. Tuple[int,int,int], Tuple[str,int] and Tuple[int,str] are all distinct types, distinguished both by the number of types in the product and the order in which they appear."(source)

What's get_products() missing 1 required positional argument: 'self'

I am trying to program for a friend of mine for fun and practice to make myself better in Python 3.6.3, I don't really understand why I got this error.
TypeError: get_products() missing 1 required positional argument: 'self'
I have done some research, it says I should initialize the object, which I did, but it is still giving me this error. Can anyone tell me where I did wrong? Or is there any better ways to do it?
from datetime import datetime, timedelta
from time import sleep
from gdax.public_client import PublicClient
# import pandas
import requests
class MyGdaxHistoricalData(object):
"""class for fetch candle data for a given currency pair"""
def __init__(self):
print([productList['id'] for productList in PublicClient.get_products()])
# self.pair = input("""\nEnter your product name separated by a comma.
self.pair = [i for i in input("Enter: ").split(",")]
self.uri = 'https://api.gdax.com/products/{pair}/candles'.format(pair = self.pair)
#staticmethod
def dataToIso8681(data):
"""convert a data time object to the ISO-8681 format
Args:
date(datetime): The date to be converted
Return:
string: The ISO-8681 formated date
"""
return 0
if __name__ == "__main__":
import gdax
MyData = MyGdaxHistoricalData()
# MyData = MyGdaxHistoricalData(input("""\nEnter your product name separated by a comma.
# print(MyData.pair)
Possibly you missed to create object of PublicClient. Try PublicClient().get_products()
Edited:
why I need the object of PublicClient?
Simple thumb rule of OOP's, if you wanna use some property(attribute) or behavior(method) of class, you need a object of that class. Else you need to make it static, use #staticmethod decorator in python.

Resources