How to get rid of "AttributeError: 'float' object has no attribute 'log2' " - pandas

Say I have a data frame with columns of min value =36884326.0, and max value =6619162563.0, which I need to plot as box plot, so I tried to log transform the values, as follows,
diff["values"] = diff['value'].apply(lambda x: (x+1))
diff["log_values"] = diff['values'].apply(lambda x: x.log2(x))
However, the above lines are throwing the error as follows,
AttributeError Traceback (most recent call last)
<ipython-input-28-fe4e1d2286b0> in <module>
1 diff['value'].max()
2 diff["values"] = diff['value'].apply(lambda x: (x+1))
----> 3 diff["log_values"] = diff['values'].apply(lambda x: x.log2(x))
~/software/anaconda/lib/python3.7/site-packages/pandas/core/series.py in apply(self, func, convert_dtype, args, **kwds)
3192 else:
3193 values = self.astype(object).values
-> 3194 mapped = lib.map_infer(values, f, convert=convert_dtype)
3195
3196 if len(mapped) and isinstance(mapped[0], Series):
pandas/_libs/src/inference.pyx in pandas._libs.lib.map_infer()
<ipython-input-28-fe4e1d2286b0> in <lambda>(x)
1 diff['value'].max()
2 diff["values"] = diff['value'].apply(lambda x: (x+1))
----> 3 diff["log_values"] = diff['values'].apply(lambda x: x.log2(x))
AttributeError: 'float' object has no attribute 'log2'
Any suggestions would be great. Thanks

You need numpy.log2 function to aplly, please, check sintaxis here.

Related

AttributeError: 'DataFrame' object has no attribute 'Vmax'

I try to solve this problem:
AttributeError: 'DataFrame' object has no attribute 'Vmax'
This is a code:
plt.figure(figsize = (15,5))
plt.plot(data.indice_tiempo, data.Vmax, label = 'Precio máximo')
plt.plot(data.indice_tiempo, data.Vmin, label = 'Precio mínimo')
plt.legend()
plt.xlabel('Fecha')
plt.ylabel('Precio')
#plt.ylim(-10,40)
plt.show()
AttributeError Traceback (most recent call last)
<ipython-input-28-e6aa3f72c6f3> in <module>
1 plt.figure(figsize = (15,5))
2
----> 3 plt.plot(data.indice_tiempo, data.Vmax, label = 'Precio máximo')
4 #plt.plot(data.indice_tiempo, data.Vmin, label = 'Precio mínimo')
5 plt.legend()
/usr/local/lib/python3.8/dist-packages/pandas/core/generic.py in __getattr__(self, name)
5485 ):
5486 return self[name]
-> 5487 return object.__getattribute__(self, name)
5488
5489 def __setattr__(self, name: str, value) -> None:
AttributeError: 'DataFrame' object has no attribute 'Vmax'
I maybe need more info to help you but I guess you are trying to plot a line representing the price of something and point the max and min price.
You can try something like this:
#Create dummy data
data_dict = {"indice_tiempo":[24,25,21,31,32,21],
"fecha":["01/06","02/06","03/06","04/06","05/06","06/06"]}
data = pd.DataFrame(data_dict)
#Set column "fecha" as index
data = data.set_index("fecha")
#Create plot
plt.figure(figsize = (15,5))
max_data = data.indice_tiempo.sort_values(ascending=False).head(1) #Filter max price data
min_data = data.indice_tiempo.sort_values(ascending=False).tail(1) #Filter min price data
plt.plot(data.index, data.indice_tiempo) #Plot line
plt.scatter(max_data.index, max_data.values, label = 'Precio máximo') #Plot max price point
plt.scatter(min_data.index, min_data.values, label = 'Precio mínimo') #Plot min price point
plt.legend()
plt.xlabel('Fecha')
plt.ylabel('Precio')
plt.show()
The error you are getting from Python is telling you that you don't have any column called "Vmax" in your dataframe.
I guess you're trying to call a function to get the max price.
For that you need to do this --> df.column_name.max() or df["column_name"].max()
Resulting PLOT

Why I can't loop xmltodict?

Ive'been trying to transform all my logs in a dict through xmltodict.parse function
The thing is, when I try to convert a single row to a variable it works fine
a = xmltodict.parse(df['CONFIG'][0])
Same to
parsed[1] = xmltodict.parse(df['CONFIG'][1])
But when I try to iterate the entire dataframe and store it on a dictionaire I get the following
for ind in df['CONFIG'].index:
parsed[ind] = xmltodict.parse(df['CONFIG'][ind])
---------------------------------------------------------------------------
ExpatError Traceback (most recent call last)
/tmp/ipykernel_31/1871123186.py in <module>
1 for ind in df['CONFIG'].index:
----> 2 parsed[ind] = xmltodict.parse(df['CONFIG'][ind])
/opt/conda/lib/python3.9/site-packages/xmltodict.py in parse(xml_input, encoding, expat, process_namespaces, namespace_separator, disable_entities, **kwargs)
325 parser.ParseFile(xml_input)
326 else:
--> 327 parser.Parse(xml_input, True)
328 return handler.item
329
ExpatError: syntax error: line 1, column 0
Can you try this?
for ind in range(len(df['CONFIG'])):
parsed[ind] = xmltodict.parse(df['CONFIG'][ind])

Error : len() of unsized object - Wilconox signed-rank test

I am running Wilconox signed-rank test on the dataset which looks like :
df = {'Year': ['2019','2018','2017', ....], 'Name':{jon, tim, luca,...}, 'SelfPromotion': [1,0,1,...]}
the script is as follows:
import pandas
from scipy.stats import mannwhitneyu
data1 = df['SelfPromotion']=1
data2 = df['SelfPromotion']=0
print(mannwhitneyu(data1, data2))
this gives me the following error:
TypeError: len() of unsized object
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
<ipython-input-30-e49d9838e5ac> in <module>
3 data1 = data['SelfPromotion']=1
4 data2 = data['SelfPromotion']=0
----> 5 print(mannwhitneyu(data11, data22))
~/opt/anaconda3/envs/shityaar/lib/python3.7/site-packages/scipy/stats/stats.py in mannwhitneyu(x, y, use_continuity, alternative)
6391 x = np.asarray(x)
6392 y = np.asarray(y)
-> 6393 n1 = len(x)
6394 n2 = len(y)
6395 ranked = rankdata(np.concatenate((x, y)))
TypeError: len() of unsized object
I have tried every possible solution for this error by looking at similar questions but unfortunately, no solution could get it to work. I would appreciate some help.
mannwhitneyu expects array like parameters and you are passing integers as args, hence the failure.
Do something like this:
In [26]: data1 = df['SelfPromotion'] == 1
In [28]: data2 = df['SelfPromotion'] == 0
In [31]: mannwhitneyu(data1, data2)
Out[31]: MannwhitneyuResult(statistic=3.0, pvalue=0.30962837708843105)

AttributeError: 'Styler' object has no attribute 'merge'

I have a problem like that, when i styled data (conditional format) with pandas, i can't merge that datas. You can find my code and error below,
Can anyone give me an advice?
CODE:
cm = sns.diverging_palette(10, 140, s=99, l=50,
n=9, center="light", as_cmap=True)
df_style1 = df_b.style.background_gradient(cmap=cm)
df_style2 = df_c.style.background_gradient(cmap=cm)
df_last = df_style1.merge(df_style2, on= 'EKSPER_ADI', how='left')
ERROR:
AttributeError Traceback (most recent call last)
<ipython-input-148-d1b2ae3dc7a6> in <module>
4 df_style1 = df_b.style.background_gradient(cmap=cm)
5 df_style2 = df_c.style.background_gradient(cmap=cm)
----> 6 df_last = df_style1.merge(df_style1, on= 'EKSPER_ADI', how='left')
AttributeError: 'Styler' object has no attribute 'merge'
I think not possible, first use merge and then apply styles:
df = df_b.merge(df_c, on= 'EKSPER_ADI', how='left')
df_style2 = df.style.background_gradient(cmap=cm)

"TypeError: only length-1 arrays can be converted to Python scalars"

This looks like a bug in pandas.Series.all to me (df is a Pandas DataFrame object, and pd is shorthand for pandas):
In [18]: df.foo.apply(lambda x: x.startswith(u'bar').head()
Out[18]:
0 True
1 False
2 True
3 True
4 False
Name: foo
In [19]: (df.baz == u'frobozz').head()
Out[19]:
0 False
1 False
2 True
3 True
4 False
Name: baz
In [20]: (type(Out[20]), type(Out[19]))
Out[20]: (pandas.core.series.Series, pandas.core.series.Series)
In [21]: pd.Series.all(Out[18], Out[19])
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
<ipython-input-310-d132f431d45f> in <module>()
----> 1 pd.Series.all(Out[18], Out[19])
/home/jones/.virtualenvs/proj/local/lib/python2.7/site-packages/pandas/core/series.pyc in f(self, *args, **kwargs)
276 #Appender(func.__doc__)
277 def f(self, *args, **kwargs):
--> 278 result = func(self, *args, **kwargs)
279 if isinstance(result, np.ndarray) and result.ndim == 0:
280 # return NumPy type
/home/jones/.virtualenvs/proj/local/lib/python2.7/site-packages/numpy/core/_methods.pyc in _all(a, axis, dtype, out, keepdims)
28 def _all(a, axis=None, dtype=None, out=None, keepdims=False):
29 return um.logical_and.reduce(a, axis=axis, dtype=dtype, out=out,
---> 30 keepdims=keepdims)
31
32 def _count_reduce_items(arr, axis):
TypeError: only length-1 arrays can be converted to Python scalars
What's going on?
This doesn't seem like a bug to me, but I don't know what you think pd.Series.all(Out[18], Out[19]) does.
>>> pd.Series.all?
Type: instancemethod
String Form:<unbound method Series.all>
File: /Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/pandas-0.10.1-py2.7-macosx-10.6-intel.egg/pandas/core/series.py
Definition: pd.Series.all(self, *args, **kwargs)
Docstring:
a.all(axis=None, out=None)
Returns True if all elements evaluate to True.
You're using the version from the class, so the first argument is being interpreted as the instance, and the second as the axis. pandas is trying to convert the second Series you're passing to an integer to make sense of it as an axis, which can't work if the array has length > 1.
From the doc, pd.Series.all appears to only take one Series object. Try this -
pd.Series.all(Out[18].append(Out[19]))