Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
3.7k 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 Answer

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...