Skip to content
bobby_dreamer

Python Errors & Solutions

python, numpy, pandas, e&s1 min read

Last updated : 08/August/2020

Events are in reverse chronological order.


Error : AttributeError: 'float' object has no attribute 'lower'

Error occurs in this line when trying to convert case to lower.

1df_tc['name'] = df_tc['name'].apply(lambda x: x.lower())
2
3# In df_tc.info(), it confirms its an object but executing above line, it fails
4name 3754 non-null object

Why this error occured because NaNs are considered as float ? I got to find that by doing

1for row in df_tc.itertuples():
2 if(type(row.name) is not str):
3 print(row.Index, type(row.name))
4
5# Output
614 <class 'float'>
721 <class 'float'>
8....
9
10df_tc.loc[14]
11# Output
12dt 2020-07-03
13name NaN
14cl 0.76
15Name: 14, dtype: object

Solution :

1df_tc['name'] = df_tc.name.str.lower()
Error : ImportError: lxml not found, please install it

Installing in Jupyter

1import sys
2!{sys.executable} -m pip install lxml
3
4**Output**
5Requirement already satisfied: lxml in c:\users\sushanth\appdata\local\programs\python\python35-32\lib\site-packages (4.5.2)

To know where its installed,

1import lxml
2lxml.__path__

This error you get even after you have installed everything,

1import lxml
2import requests
3import html5lib
4import BeautifulSoup4
5
6import sys
7!{sys.executable} -m pip install BeautifulSoup4

When you execute the below code,

1url = 'https://en.wikipedia.org/wiki/List_of_elevator_accidents'
2tables = pd.read_html(url)
3
4print(tables)
5
6#Output :
7ImportError: lxml not found, please install it

Solution :

You have to Restart Kernel in Jupyter

Error : tuple indices must be integers or slices, not str

When looping dataframe trying to get a value from dataframe cell, got the error.

1row[rd_Qcolname]

Solution :

1getattr(row, rd_Qcolname)
Error : TypeError: unorderable types: datetime.date() > str()

Date : 17/April/2020

When executing the below line in pandas, got that error.

1df_temp[df_temp['ts'] > dt_3d]
2
3# Error :
4# TypeError: unorderable types: datetime.date() > str()

Analysis

1print(type(df_temp['ts'])) # <class 'pandas.core.series.Series'>
2print(type(df_temp['ts'][0])) # <class 'datetime.date'>
3print(type(dt_3d)) # <class 'str'>

Update 1 : Updated like below, still got an error.

1df_temp[df_temp['ts'] > pd.to_datetime(dt_3d)]
2
3# Error :
4# TypeError: Cannot compare type 'Timestamp' with type 'date'

Solution :

Update 2 : df_temp[df_temp['ts'] > pd.to_datetime(dt_3d).date()]

Worked

Error : TypeError: 'tuple' object is not callable
1for i in range(10):
2 print(i, end =" ")
3
4---------------------------------------------------------------------------
5TypeError Traceback (most recent call last)
6<ipython-input-324-e27c647546fc> in <module>
7----> 1 for i in range(10):
8 2 print(i, end =" ")
9
10TypeError: 'tuple' object is not callable

Solution : This took some time to solve. I had used keyword range as a variable.

1print(range)
2
3(201913, 201915)

Solution is del range and then the loop worked.

1for i in range(10):
2 print(i, end =" ")
3
40 1 2 3 4 5 6 7 8 9