AttributeError:Float' object has no attribute log /TypeError: ufunc 'log' not supported for the input types - python-3.x

I have a series of fluorescence intensity data in a column ('2.4M'). I tried to create a new column 'ln_2.4M' by taking the ln of column '2.4M' I got an error:
AttributeError: 'float' object has no attribute 'log'
df["ln_2.4M"] = np.log(df["2.4M"])
I tried using a for loop to iterate the log over each fluorescence data in the column "2.4M":
ln2_4M = []
for x in df["2.4M"]:
ln2_4M = np.log(x)
print(ln2_4M)
Although it printed out ln2_4M as log of column "2.4M" correctly, I am unable to use the data because it gave alongside a TypeError:
ufunc 'log' not supported for the input types, and the inputs could not be safely coerced to any supported types according to the casting rule ''safe'
Not sure why? - Any help at understanding what is happening and how to fix this problem is appreciated. Thanks

.
I then tried using the method below and it worked:
df["2.4M"] = pd.to_numeric(df["2.4M"],errors = 'coerce')
df["ln_24M"] = np.log(df["2.4M"])

Related

AttributeError: 'int' object has no attribute 'pop'

When writing list inside tuple, getting error:
AttributeError: 'int' object has no attribute 'pop'
fruits=('apple','banana', ['kiwi','grapes'])
fruits[1].pop()
fruits[1].append('rice')
print(fruits)
I am getting:
str object has no attribute pop
which is what I was expecting since fruits[1] is 'banana' which is a string and strings do not have a pop method.
If you look at the next line you will run into a similar issue since strings also don't have an append method.
If you remove both of those lines you shouldn't have any errors with just the list inside of the tuple.
fruits = ('apple','banana', ['kiwi','grapes'])
print(fruits)
Your issue is coming from you trying to pop a string, in the second line of your code - not from adding a list in the tuple. Now as tuples are immutable in python, you will need to convert it into a list if you wish to start modyfing what is inside of it. A simple example using your code is below.
fruits=('apple','banana', ['kiwi','grapes'])
fruits_list = list(fruits)
fruits_list.pop(1)
fruits_list.insert(1, 'rice')
fruits = tuple(fruits_list)
print(fruits)
You can read more about it here: pop/remove items out of a python tuple

ColumnTransformer object has no attribute shape error

My data file (CSV) contains categorical and non-categorical variables. To perform cox proportional hazard (CPH) I applied OneHotEncoder on two categorical variables (study_category and patient_category). I am getting the following error on the line where I am trying to fit the CPH model. I am passing three parameters: dataframe, duration column (), event column() to cph.fit() method. I googled the error but could not found something useful. I am using CPH first time, any help to fix the issue will be appreciated.
Error:
AttributeError: 'ColumnTransformer' object has no attribute 'shape'
My python code:
def meth():
dataset = pd.read_csv("C:/Users/XYZ/CTR_Project/CPH.csv")
dataset=dataset.loc[:,
['study_Category','patient_Category','Diff_time','Events']]
X=dataset.loc[:,['study_Category','patient_Category','Diff_time','Events']]
colm_transf=make_column_transformer((OneHotEncoder(),
['study_Category','patient_Category']),remainder='passthrough')
colm_transf.fit_transform(X)
cph= CoxPHFitter()
cph.fit(colm_transf,duration_col='Diff_time', event_col='Events')
cph.print_summary()

Getting an error when calculating Z score

I am trying to find the outliers in my dataset and remove them. So I did the following:
z_scores = stats.zscore(dataset_sex)
abs_z_scores = np.abs(z_scores)
filtered_entries = (abs_z_scores < 3).all(axis=1)
new_df = dataset_sex[filtered_entries]
new_df.head()
but I got this error:
TypeError: unsupported operand type(s) for /: 'str' and 'int'
The error seems to generate from the first line of code (z_scores = stats.zscore(dataset_sex)). I don't understand why. How can I fix this?
This comes from some of your data in the columns being strings (in python terms 'str').
When it comes from working out the z-score, it will have to divide the mean with a standard deviation. One of the columns is a string like 'M' or 'F' for sex, or strings like '1,232.23' not converted to floats, and z-scoring does not work for that.
My first suggestion is to check that they are all numbers.
df.dtypes
will show you what types they are and then convert them to numeric.
Post a little of the data (a couple of rows) and we can help you.

Convert a pandas Timestamp list

In my variable 'Datelist3' there is a pandas Timestamp list, in the following format:
[Timestamp('2019-12-04 09:00:00+0100', tz='Europe/Rome'), Timestamp('2019-12-04 09:30:00+0100', tz='Europe/Rome'), ....]
I'm having difficulty converting this list to a datetime string list, in this format:
['2019-12-04 09:00:00', '2019-12-04 09:30:00', .....]
I did these tests:
Datelist3.to_datetime # -> error: 'list' object has no attribute 'to_datetime'
Datelist3.dt.to_datetime # -> error: 'list' object has no attribute 'dt'
Datelist3.to_pydatetime() # -> error: 'list' object has no attribute 'to_pydatetime()'
Datelist3.dt.to_pydatetime() # -> error: 'list' object has no attribute 'dt'
I got to the variable 'Datelist3' with the following statement:
Datelist3 = quoteIntraPlot.index.tolist()
If I this instruction changes it to:
Datelist3 = quoteIntraPlot.index.strftime("%Y-%m-%d %H:%M:%S").tolist()
That's exactly what I want to achieve.
The problem is that out of 10 times, 6-7 times is ok and 3-4 times it gives me an error: " 'Index' object has no 'strftime' ". It's very strange. How could I solve this problem?
If your data is well formed, this would work :
time_list = [Timestamp('2019-12-04 09:00:00+0100', tz='Europe/Rome'), Timestamp('2019-12-04 09:30:00+0100', tz='Europe/Rome'), ....]
str_list = [t.strftime("%Y-%m-%d %H:%M:%S") for t in time_list]
However, if you face the same error as before, it means that not all your index are timestamps. In this case, you need to clean your data first.

'>=' not supported between instances of 'tuple' and 'datetime.datetime'

I am using Python3 and have the following code (partial code).
tsret = pd.DataFrame(index=tslag.index)
start_date=datetime.datetime(2011,1,10)
tsret = tsret[tsret.index >= start_date]
tslag in web data from yahoo/google
I have imported pandas and date frame. I am getting the following error
TypeError: '>=' not supported between instances of 'tuple' and 'datetime.datetime'.
I understand that tsret.index is a tuple whereas start_date is of type datetime.datetime, hence I am not able to compare these two. How do I overcome the error? Please help.

Resources