In programming, you must know your data types (classes).

You wanted to use this cast method:

Column.cast(dataType: Union[pyspark.sql.types.DataType, str]) → pyspark.sql.column.Column

You must know your data types (classes)

A.cast(B) → C

A: The parent class of the method. It's pyspark.sql.column.Column class (a.k.a. pyspark.sql.Column).
B: Inputs for the method. According to the above documentation line, you can use either pyspark.sql.types.DataType or str class.
C: The output class. According to the above documentation line, it's pyspark.sql.column.Column.

In your case, your actual A is of wrong data type to be chained with cast.
In other words, the class of A, doesn't have a cast method.
In other words, as your A = number1-number2/number3*number4 which means it's a float class object , the error precisely tells you that "'float' object has no attribute 'cast'".


Regarding the translation of your Python code to PySpark, it doesn't really make sense. It's because you do the calculation for variables. I mean, only 2 variables. The pyspark.sql.Column objects are called columns, because they contain many different values. So you must create a dataframe (just columns are not enough for actual calculations) and put some values in columns in order to make sense of translating the formula to PySpark.

I'll just show you how it may work if you had just one row.

Creating Spark session (not needed if you run the code in PySpark shell):

from pyspark.sql import SparkSession, functions as F
spark = SparkSession.builder.getOrCreate()

Creating and printing the dataframe:

df = spark.createDataFrame([(2, 50)], ['null_count', 'total'])
df.show()
# +----------+-----+
# |null_count|total|
# +----------+-----+
# |         2|   50|
# +----------+-----+

Adding a column using your logic, but working with Spark columns instead of Python variables.

df = df.withColumn('d', F.round(100 - F.col('null_count') / F.col('total') * 100, 2).cast('float'))
df.show()
# +----------+-----+----+
# |null_count|total|   d|
# +----------+-----+----+
# |         2|   50|96.0|
# +----------+-----+----+

Python's round was also replaced with PySpark's F.round, because the argument to the function will now be Spark column expression (i.e. a column) as opposed to a single value or variable.

Answer from ZygD on Stack Overflow
Top answer
1 of 1
1

In programming, you must know your data types (classes).

You wanted to use this cast method:

Column.cast(dataType: Union[pyspark.sql.types.DataType, str]) → pyspark.sql.column.Column

You must know your data types (classes)

A.cast(B) → C

A: The parent class of the method. It's pyspark.sql.column.Column class (a.k.a. pyspark.sql.Column).
B: Inputs for the method. According to the above documentation line, you can use either pyspark.sql.types.DataType or str class.
C: The output class. According to the above documentation line, it's pyspark.sql.column.Column.

In your case, your actual A is of wrong data type to be chained with cast.
In other words, the class of A, doesn't have a cast method.
In other words, as your A = number1-number2/number3*number4 which means it's a float class object , the error precisely tells you that "'float' object has no attribute 'cast'".


Regarding the translation of your Python code to PySpark, it doesn't really make sense. It's because you do the calculation for variables. I mean, only 2 variables. The pyspark.sql.Column objects are called columns, because they contain many different values. So you must create a dataframe (just columns are not enough for actual calculations) and put some values in columns in order to make sense of translating the formula to PySpark.

I'll just show you how it may work if you had just one row.

Creating Spark session (not needed if you run the code in PySpark shell):

from pyspark.sql import SparkSession, functions as F
spark = SparkSession.builder.getOrCreate()

Creating and printing the dataframe:

df = spark.createDataFrame([(2, 50)], ['null_count', 'total'])
df.show()
# +----------+-----+
# |null_count|total|
# +----------+-----+
# |         2|   50|
# +----------+-----+

Adding a column using your logic, but working with Spark columns instead of Python variables.

df = df.withColumn('d', F.round(100 - F.col('null_count') / F.col('total') * 100, 2).cast('float'))
df.show()
# +----------+-----+----+
# |null_count|total|   d|
# +----------+-----+----+
# |         2|   50|96.0|
# +----------+-----+----+

Python's round was also replaced with PySpark's F.round, because the argument to the function will now be Spark column expression (i.e. a column) as opposed to a single value or variable.

Discussions

'float' object has no attribute...(beginner)
Hello there, I have written a simple function to find the area of a square: def area_of_square(): side_length = float(raw_input("Length in cm: ")) area = side_length ** 2 if side_length.isalpha(): print “this is not a number” else: print area When I attempt to run this, after I input the ... More on discuss.codecademy.com
🌐 discuss.codecademy.com
1
0
August 14, 2020
python - Why I get AttributeError: 'float' object has no attribute '3f'? - Data Science Stack Exchange
I am getting this error: AttributeError: 'float' object has no attribute '3f' I don't understand why I am getting it, I am following the example straight from the book "applied text analysis" The... More on datascience.stackexchange.com
🌐 datascience.stackexchange.com
December 10, 2019
python - Numpy AttributeError: 'float' object has no attribute 'exp' - Stack Overflow
this error message is very misleading: the problem is that the dtype is in fact numpy.object, but the message says numpy.float64 has no attribute log10, or whatever arithmetic method 2017-01-03T15:59:42.163Z+00:00 More on stackoverflow.com
🌐 stackoverflow.com
AttributeError: 'float' object has no attribute 'upper'
Exception in thread Thread-1337: ... "/usr/local/lib/python3.8/dist-packages/pytz/init.py", line 170, in timezone if zone.upper() == 'UTC': AttributeError: 'float' object has no attribute 'upper'... More on github.com
🌐 github.com
36
October 19, 2022
🌐
Bobby Hadz
bobbyhadz.com › blog › python-attributeerror-float-object-has-no-attribute
AttributeError: 'float' object has no attribute 'X' (Python) | bobbyhadz
Instead of accessing the round() function on the float, e.g.my_float.round(), we have to pass the floating point number as an argument to the round() function, e.g. round(3.5). If you need to check whether an object contains an attribute, use the hasattr function. ... Copied!example = 5.4 if hasattr(example, 'round'): print(example.round) else: print('Attribute is not present on object') # 👉️ This runs
🌐
DEV Community
dev.to › olakusibe › solving-attributeerror-float-object-has-no-attribute-rint-4la
Solving AttributeError: 'float' object has no attribute 'rint' - DEV Community
July 26, 2022 - import pandas as pd # an example of extracted data extracted_data = pd.DataFrame({ 'Brand': ['Gucci', 'Nike', 'Adidas', 'Hermes', 'Zara'], 'year_of_manufacture': [2010, 1999, '2012', 2011, '2008'], # mixed data type 'price': ['4034.203', 5000.00, '7450.17567', 3023.004, '4901.32345'] # mixed data type }) # cast specific columns to a desired data type cast_to_type = { 'year_of_manufacture': int, 'price': float } extracted_data = extracted_data.astype(cast_to_type) # do the transfrom extracted_data['price'] = extracted_data['price'].round(2) print(extracted_data)
Find elsewhere
🌐
GitHub
github.com › ranaroussi › yfinance › issues › 1096
AttributeError: 'float' object has no attribute 'upper' · Issue #1096 · ranaroussi/yfinance
October 19, 2022 - Exception in thread Thread-1337: ... "/usr/local/lib/python3.8/dist-packages/pytz/init.py", line 170, in timezone if zone.upper() == 'UTC': AttributeError: 'float' object has no attribute 'upper'...
Author   gib-uk
🌐
GitHub
github.com › ultralytics › yolov5 › issues › 1049
AttributeError: 'float' object has no attribute 'round' · Issue #1049 · ultralytics/yolov5
September 26, 2020 - File "train.py", line 459, in <module> train(hyp, opt, device, tb_writer) File "train.py", line 250, in train accumulate = max(1, np.interp(ni, xi, [1, nbs / total_batch_size]).round()) AttributeError: 'float' object has no attribute 'round' Reactions are currently unavailable ·
Author   universewill
🌐
Unmet Hours
unmethours.com › question › 68535 › error-float-object-has-no-attribute-apply
Error: 'float' object has no attribute 'apply' - Unmet Hours
This variable is set a few lines above with the .get_actuator_handle method, which returns -1 if the object can't be found or one of the parameters (state, component_type, control_type, actuator_key) are set incorrectly. Have you initialized state = api.state_manager.new_state() before this? Otherwise "LIV1XWIN_SCH" may not be the correct schedule name.
🌐
GitHub
github.com › vandijklab › cell2sentence › issues › 4
AttributeError: 'float' object has no attribute 'item' · Issue #4 · vandijklab/cell2sentence
October 12, 2024 - ------------------------------... normalized_expression_matrix, sample_size) 278 "slope": [linear_reg.coef_.item()], 279 "intercept": [linear_reg.intercept_.item()], --> 280 "r_squared": [r_squared_score.item()], 281 "pearson_r_statistic": [pearson_r_score.statistic.item()], 282 "pearson_r_pvalue": [pearson_r_score.pvalue.item()], AttributeError: 'float' object has no attribute ...
Author   ayyucedemirbas
Top answer
1 of 3
26

The error points to this line:

df['content'] = df['content'].apply(lambda x: " ".join(x.lower() for x in x.split() \
                                    if x not in stop_words))

split is being used here as a method of Python's built-in str class. Your error indicates one or more values in df['content'] is of type float. This could be because there is a null value, i.e. NaN, or a non-null float value.

One workaround, which will stringify floats, is to just apply str on x before using split:

df['content'] = df['content'].apply(lambda x: " ".join(x.lower() for x in str(x).split() \
                                    if x not in stop_words))

Alternatively, and possibly a better solution, be explicit and use a named function with a try / except clause:

def converter(x):
    try:
        return ' '.join([x.lower() for x in str(x).split() if x not in stop_words])
    except AttributeError:
        return None  # or some other value

df['content'] = df['content'].apply(converter)

Since pd.Series.apply is just a loop with overhead, you may find a list comprehension or map more efficient:

df['content'] = [converter(x) for x in df['content']]
df['content'] = list(map(converter, df['content']))
2 of 3
9

split() is a python method which is only applicable to strings. It seems that your column "content" not only contains strings but also other values like floats to which you cannot apply the .split() mehthod.

Try converting the values to a string by using str(x).split() or by converting the entire column to strings first, which would be more efficient. You do this as follows:

df['column_name'].astype(str)
🌐
GitHub
github.com › Comfy-Org › ComfyUI › issues › 8371
AttributeError: 'float' object has no attribute 'to' · Issue #8371 · Comfy-Org/ComfyUI
June 1, 2025 - 2025-06-01T17:10:14.916492 - loaded partially 4779.476829624176 4779.476829528809 0 2025-06-01T17:10:16.075030 - Requested to load WanVAE 2025-06-01T17:10:17.693881 - loaded completely 1040.7591247558594 242.02829551696777 True 2025-06-01T17:10:32.256528 - model weight dtype torch.float8_e4m3fn, manual cast: torch.float16 2025-06-01T17:10:32.258526 - model_type FLOW 2025-06-01T17:11:21.782072 - Requested to load WAN21_Vace 2025-06-01T17:11:23.234997 - loaded partially 237.67314464721676 237.66943359375 0 2025-06-01T17:11:23.433590 - 0%| | 0/20 [00:00<?, ?it/s]2025-06-01T17:11:23.571490 - 0%| |
Author   wmdk200086
🌐
GitHub
github.com › nalepae › pandarallel › issues › 102
AttributeError: 'float' object has no attribute 'a' · Issue #102 · nalepae/pandarallel
July 22, 2020 - RemoteTraceback Traceback (most ... File "<ipython-input-4-ae12c3a75aca>", line 2, in func return math.sin(x.a**2) + math.sin(x.b**2) AttributeError: 'float' object has no attribute 'a' """ The above exception was the direct cause of the following exception: AttributeError ...
Author   mustafakhalidcs
🌐
McNeel Forum
discourse.mcneel.com › scripting
'Vector3d' object has no attribute '__float__' - Scripting - McNeel Forum
November 10, 2019 - Hello everyone! I’m trying to simulate a script from the RhinoPythonPrimer101.pdf that can be found here. On page 43 there is an example to iteratively shorten a curve, under the heading Nested Lists. Here is my code and I’m getting this error: Message: ‘Vector3d’ object has no attribute ‘float’ (Before that I was getting a “Expected float, got point3D” message, so I tried to change vm to float(vm) on line 15…)
🌐
GitHub
github.com › celery › celery › issues › 5175
AttributeError: 'float' object has no attribute 'items' · Issue #5175 · celery/celery
November 15, 2018 - on running celery -A proj worker throws this error [2018-11-15 12:02:53,607: CRITICAL/MainProcess] Unrecoverable error: AttributeError("'float' object has no attribute 'items'",) Traceback (most recent call last): File "/user/lib/python3...
Author   noufalvlpr
🌐
Esri Community
community.esri.com › t5 › python-questions › attributeerror-float-object-has-no-attribute › td-p › 39532
Solved: AttributeError: 'float' object has no attribute 's... - Esri Community
December 12, 2021 - I'm hoping someone can tell me whats going on with the below bit of code. I keep getting the error AttributeError: 'float' object has no attribute 'setValue' and I'm not sure how to resolve it. Can someone please explain? NONFLOOD = "GRIDfield" NF1curs = arcpy.UpdateCursor(BuildingGridSe...