python 类型转换 object to datetime64[ns],timedelta64[ns] to float

简介: python 类型转换 object to datetime64[ns],timedelta64[ns] to float

271252507a86405fb1738b842cc35719.png

一、object 变成 datetime64[ns]



如上图:原本这个dateframe当中的Date数据的类型是int

加上下面这句话

alldfgbcountrysumv2换成自己的dataframe名即可:

alldfgbcountrysumv2['Date']=pd.to_datetime(alldfgbcountrysumv2['Date']).dt.normalize()

03703a9357c1443d917260d4822be181.png


二、timedelta64[ns] 变成 float



将timedelta64[ns]类型变成float类型:

4eea56cbe08d403cad337818c9b07264.png

allregiondatesum['Permildays']=allregiondatesum['Permildays'].dt.days.replace()

e7ab5f7981704912a32cb109e4f92f43.png


三、去掉Timestamp



将下面这组数组变成只显示’2020-01-28’,类型变成object

36b6e4b1d18c484a93c71e069c386b96.png

.add_xaxis(xaxis_data=pd.to_datetime(y['Date']).dt.strftime('%Y-%m-%d').drop_duplicates().to_list()


四、获取datetime64[ns]的年,月,日



moviesdf['release_date'].head(1)

4829bea361004e6393ac4d3a0b730291.png

获取年份:

moviesdf['releaseyear']=pd.to_datetime(moviesdf['release_date']).dt.year
moviesdf.head(3)

66414ef9dbd24c6bbd10e1894481eeb3.png


相关文章
|
索引 Python
Python错误 - 'list' object is not callable 的问题定位与解决
出现编程问题并不可怕,关键在于是否可以从中学习与成长。遇到'list' object is not callable这样的错误,我们不仅需要学会应对,更需要了解其背后的原因,避免类似的问题再次出现。记住,Python的强大功能和灵活性同时也意味着我们需要对其理解更准确,才能更好的使用它。
1715 70
|
C++ Python
Python Tricks--- Object Comparisons:“is” vs “==”
Python Tricks--- Object Comparisons:“is” vs “==”
214 1
|
API C++ Python
【Azure 应用服务】Python fastapi Function在Azure中遇见AttributeError异常(AttributeError: 'AsgiMiddleware' object has no attribute 'handle_async')
【Azure 应用服务】Python fastapi Function在Azure中遇见AttributeError异常(AttributeError: 'AsgiMiddleware' object has no attribute 'handle_async')
287 0
|
Python
Python中的float语句
Python中的float语句
1546 3
|
数据处理 Python
【Python】解决tqdm ‘module‘ object is not callable
在使用tqdm库时遇到的“'module' object is not callable”错误,并给出了正确的导入方式以及一些使用tqdm的常见示例。
903 1
|
JSON 数据格式 Python
【python】解决json.dump(字典)时报错Object of type ‘float32‘ is not JSON serializable
在使用json.dump时遇到的“Object of type ‘float32’ is not JSON serializable”错误的方法,通过自定义一个JSON编码器类来处理NumPy类型的数据。
1151 1
|
Python
【Python】已解决:(Python xlwt写入Excel报错)AttributeError: ‘function’ object has no attribute ‘font’
【Python】已解决:(Python xlwt写入Excel报错)AttributeError: ‘function’ object has no attribute ‘font’
404 0
|
Python
【Python】已解决:(Python写入Excel表格报错)‘NoneType’ object has no attribute ‘write’
【Python】已解决:(Python写入Excel表格报错)‘NoneType’ object has no attribute ‘write’
1476 0
|
XML 数据格式 Python
【Python】已解决:AttributeError: ‘function’ object has no attribute ‘ELement’
【Python】已解决:AttributeError: ‘function’ object has no attribute ‘ELement’
960 0
|
API Python
【Python】已解决:AttributeError: ‘TfidfVectorizer’ object has no attribute ‘get_feature_names_out’
【Python】已解决:AttributeError: ‘TfidfVectorizer’ object has no attribute ‘get_feature_names_out’
730 0

热门文章

最新文章

推荐镜像

更多