Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
4.1k views
in Technique[技术] by (71.8m points)

python 3.x - When updating dataframe, date type changes to int type

I seem to have a problem when updating my Dataframe table using this

Table.update(other)

'Table' and 'other' have the exact same column labels. it used to work just fine, but now it started showing this for example:

2021-04-12T00:00:00.000000000 <class 'numpy.datetime64'>
1618185600000000000 <class 'int'>

the first result is taken from "other" and after update, it becomes int as seen above.

These are the results of Table.dtypes:

ID                      object
Column1                 object
Column2                 object
Column3                 object
Column4                 object
Column5                 object
Column6                 object
Column7                 object
dtype: object

And the results of other.dtypes

ID                            object
Column1                       float64
Column2                       datetime64[ns]
Column3                       datetime64[ns]
Column4                       float64
Column5                       datetime64[ns]
Column6                       datetime64[ns]
Column7                       object
dtype: object

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

When updating a dataframe with another dataframe you need to have them being the same dtype in the columns.

to_numeric() - provides functionality to safely convert non-numeric types (e.g. strings) to a suitable numeric type. (See also to_datetime() and to_timedelta().)

astype() - convert (almost) any type to (almost) any other type (even if it's not necessarily sensible to do so). Also allows you to convert to categorial types (very useful).

infer_objects() - a utility method to convert object columns holding Python objects to a pandas type if possible.

convert_dtypes() - convert DataFrame columns to the "best possible" dtype that supports pd.NA (pandas' object to indicate a missing value).

see Change column type in pandas for more information on these instructions

However you need to convert the columns to dates. specifically datetime64[ns] for this you need

df['Column2'] = pd.to_datetime(df['Column2'])
df['Column3'] = pd.to_datetime(df['Column3'])
df['Column5'] = pd.to_datetime(df['Column5'])
df['Column6'] = pd.to_datetime(df['Column6'])

remember to not update until they both have the same dtypes


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...