Error: astype() got an unexpected keyword argument categories

2024/10/8 0:27:21
 df = pd.DataFrame(['A+', 'A', 'A-', 'B+', 'B', 'B-', 'C+', 'C', 'C-', 'D+', 'D'],index=['excellent', 'excellent', 'excellent', 'good', 'good', 'good', 'ok', 'ok', 'ok', 'poor', 'poor'])df.rename(columns={0: 'Grades'}, inplace=True)df

I am trying to create an ordered category from the above dataframe using the following code -

df = df['Grades'].astype('category',categories=['D', 'D+', 'C-', 'C', 'C+', 'B-', 'B', 'B+', 'A-', 'A', 'A+'],ordered=True)

However it gives the error : astype() got an unexpected keyword argument 'categories'.

Answer

From pandas 0.25+ are removed these arguments:

Removed the previously deprecated ordered and categories keyword arguments in astype (GH17742)


In newer pandas versions is necesary use CategoricalDtype and pass to astype:

from pandas.api.types import CategoricalDtypecats = ['D', 'D+', 'C-', 'C', 'C+', 'B-', 'B', 'B+', 'A-', 'A', 'A+']
cat_type = CategoricalDtype(categories=cats, ordered=True)
df['Grades'] = df['Grades'].astype(cat_type)
print (df)Grades
excellent     A+
excellent      A
excellent     A-
good          B+
good           B
good          B-
ok            C+
ok             C
ok            C-
poor          D+
poor           D

Or use Categorical:

df['Grades'] = pd.Categorical(df['Grades'], categories=cats, ordered=True)
print (df)Grades
excellent     A+
excellent      A
excellent     A-
good          B+
good           B
good          B-
ok            C+
ok             C
ok            C-
poor          D+
poor           D
https://en.xdnf.cn/q/70179.html

Related Q&A

python: regular expression search pattern for binary files (half a byte)

I am using the following regular expression pattern for searching 0xDEAD4FAD in a binary file:my_pattern = re.compile(b"\xDE\xAD\x4F\xAD")but how do I generalize the search pattern for search…

pandas: selecting rows in a specific time window

I have a dataset of samples covering multiple days, all with a timestamp. I want to select rows within a specific time window. E.g. all rows that were generated between 1pm and 3 pm every day.This is a…

How to visualize (dendrogram) a dictionary of hierarchical items?

This is my first time of doing visualization from hierarchical data in dictionary format with Python. Last part of the data looks like this:d = {^2820: [^391, ^1024], ^2821: [^759, w, ^118, ^51], ^2822…

How to unfocus (blur) Python-gi GTK+3 window on Linux

What I want to do and whyI want my window to unfocus, so the previous focused window is selected.Why? I want to interact with the previously selected window (from other programs). My current plan is: …

SyntaxError: multiple exception types must be parenthesized

I am a beginner and have a problem after installing pycaw for the audio control using python, on putting the basic initialization code for pycaw, i get the following error:- Traceback (most recent call…

Python find difference between file paths

I have a bunch of file paths, such as:path1 = "./base/folder1/subfolder" path2 = "./base/folder2/"I am trying to write a function that can give me the relative difference between th…

Update range of colorbar in matplotlib

I want to update a contourf plot within a function, which works fine. However, the range of the data changes and I therefore also have to update the colorbar. That is where I fail to do so.Please see f…

Anaconda - arrow keys not work well in python shell

I installed Anaconda3 on manjaro (with i3wm and Urxvt). When I go into python interpreter, it is OK to type python script and execute. But when key arrows are pressed to call up history, everything mes…

couldnt remove origin point in matplotlib polycollection

I have tried an example with PolyCollection from matplotlib tutorials and noticed one strange thing. I couldnt remove this points from axes origin see fig. How do I manage this?from mpl_toolkits.mplot…

How to read .odt using Python? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.Want to improve this question? Add details and clarify the problem by editing this post.Closed 3 years ago.The com…