Try this instead,

  print(
"{:.3f}% {} ({} sentences)".format(pcent, gender, nsents)
)

Refer the latest docs for more examples and check the Py version!

Answer from Aditya on Stack Exchange
🌐
GitHub
github.com › choderalab › espaloma › issues › 130
AttributeError: 'float' object has no attribute 'value_in_unit' (and others) · Issue #130 · choderalab/espaloma
September 26, 2022 - Right now CI is failing due to the new openff toolkit using a different package for units. The openff-unit package provides a way to support both using openmm.units and openff.units. For now in PR #129 I will just pin to the older toolki...
Author   mikemhenry
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)
Find elsewhere
🌐
Bobby Hadz
bobbyhadz.com › blog › python-attributeerror-float-object-has-no-attribute
AttributeError: 'float' object has no attribute 'X' (Python) | bobbyhadz
The Python "AttributeError: 'float' object has no attribute" occurs when we try to access an attribute that doesn't exist on a floating-point number, e.g.
🌐
Stack Overflow
stackoverflow.com › questions › 70231543 › i-receive-this-attribute-error-attributeerror-float-object-has-no-attribute
I receive this attribute error "AttributeError: 'float' object has no attribute 'type_of_payment'" I am fairly new to python so I apologize if dumb - Stack Overflow
Explore Stack Internal ... Payment = Enum('Payment', ['food', 'consulting', 'honoraria', 'travel', 'services', 'gift', 'education','entertainment', 'grant', 'space_rental', 'royalties', 'investment', 'charity']) # interp. types/natures of payments or transactions #examples are redundant for enumerations GeneralPaymentTypeDistribution = NamedTuple ('GeneralPaymentTypeDistribution', [('total_amount', float), # in range[0, ...) ('type_of_payment', Payment), ('degree', str)]) # interp.
Top answer
1 of 9
65

The answer is already provided in the comments by @mattdmo and @tdelaney:

  • NumPy 1.20 (release notes) deprecated numpy.float, numpy.int, and similar aliases, causing them to issue a deprecation warning

  • NumPy 1.24 (release notes) removed these aliases altogether, causing an error when they are used

In many cases you can simply replace the deprecated NumPy types by the equivalent Python built-in type, e.g., numpy.float becomes a "plain" Python float.

For detailed guidelines on how to deal with various deprecated types, have a closer look at the table and guideline in the release notes for 1.20:

...

To give a clear guideline for the vast majority of cases, for the types bool, object, str (and unicode) using the plain version is shorter and clear, and generally a good replacement. For float and complex you can use float64 and complex128 if you wish to be more explicit about the precision.

For np.int a direct replacement with np.int_ or int is also good and will not change behavior, but the precision will continue to depend on the computer and operating system. If you want to be more explicit and review the current use, you have the following alternatives:

  • np.int64 or np.int32 to specify the precision exactly. This ensures that results cannot depend on the computer or operating system.
  • np.int_ or int (the default), but be aware that it depends on the computer and operating system.
  • The C types: np.cint (int), np.int_ (long), np.longlong.
  • np.intp which is 32bit on 32bit machines 64bit on 64bit machines. This can be the best type to use for indexing.

...

If you have dependencies that use the deprecated types, a quick workaround would be to roll back your NumPy version to 1.24 or less (as suggested in some of the other answers), while waiting for the dependency to catch up. Alternatively, you could create a patch yourself and open a pull request, or monkey patch the dependency in your own code.

2 of 9
39

In the 1.24 version:

The deprecation for the aliases np.object, np.bool, np.float, np.complex, np.str, and np.int is expired (introduces NumPy 1.20). Some of these will now give a FutureWarning in addition to raising an error since they will be mapped to the NumPy scalars in the future.

pip install "numpy<1.24" to work around it.

In [1]: import numpy as np

In [2]: np.__version__
Out[2]: '1.23.5'

In [3]: np.float(3)
<ipython-input-3-8262e04d58e1>:1: DeprecationWarning: `np.float` is a deprecated alias for the builtin `float`. To silence this warning, use `float` by itself. Doing this will not modify any behavior and is safe. If you specifically wanted the numpy scalar type, use `np.float64` here.
Deprecated in NumPy 1.20; for more details and guidance: https://numpy.org/devdocs/release/1.20.0-notes.html#deprecations
  np.float(3)
Out[3]: 3.0
Top answer
1 of 2
5

Clearly at some point you're calling lcd_string with something that's not a string, but a float. So, when it does this:

message = message.center(LCD_WIDTH," ")

… that fails, because float objects have no center method.

The fact that it's failing in the center case means it must be one of the places where you call lcd_string(<something>, 2). My first guess would be lcd_string(avgspeed,2), because avgspeed sure sounds like something that would be a float rather than a string.

(Now that you've posted the traceback, it tells you directly what I had to guess. The line that fails is exactly the one I said it would be, and the line that called it is exactly the one I suspected would be calling it. And this is why you should always look at the tracebacks, and paste them to your SO questions—otherwise, you have to scan the code and guess.)


The reason it seems to work if you replace that with just print message is that the print command automatically stringifies its arguments, so even if you pass it a float, it will print out that float.

If you want your lcd_string function to work the same way, you can do that pretty easily by adding one line to the start of it:

message = str(message)

Alternatively, change your code to never call lcd_string with anything else. So:

lcd_string(str(avgspeed), 2)

… or, if you want more control over the formatting, something like this:

lcd_string(format(avgspeed, '.2f'), 2) 

Since any developer is bound to screw that up sometimes (at least I would, and you obviously would too), and you have a hard time debugging those screwups yourself, you might want to add a line at the top of the function to give you better error messages, like:

assert isinstance(message, str)
2 of 2
0

lcd_string requires it's first parameter to be a string

Check out this post:

lcd module raspberry pi

🌐
GitHub
github.com › ranaroussi › yfinance › issues › 1096
AttributeError: 'float' object has no attribute 'upper' · Issue #1096 · ranaroussi/yfinance
October 19, 2022 - Any ideas on the error below? My code works fine on windows locally on my laptop, it basically pulls data for a group of 5000 stocks 1 by 1. When i try to run it onlinus most of the symbols work fine, but i seem to get this error a handf...
Author   gib-uk