【问题标题】:Convert the column type from object to date format - python将列类型从对象转换为日期格式 - python
【发布时间】:2021-04-29 09:52:14
【问题描述】:

我已经连接了两个数据框,连接前的列类型是日期时间,但连接后的列类型变成了对象,当我导出到excel时它完全改变了!

这是两个数据框:

df_last_month:

project number status Project Naming CF VPC CO MA
A Planned DH 2021-01-26 2021-03-16 2021-11-16 2023-10-10
B frozen DH 2017-12-01 2018-12-18 2019-07-26 2022-02-18
C Planned DH 2017-12-01 2018-12-18 2019-07-26 2022-02-18
D Planned HH 2017-12-01 2018-12-18 2019-07-26 2022-02-18

df_current_month:

project number status Project Naming CF VPC CO MA
A Planned DH 2021-01-10 2021-03-16 2021-09-16 2023-10-10
B frozen DH 2017-12-01 2018-12-18 2019-07-26 2022-02-18
E completed DH 2017-12-01 2018-12-18 2019-07-26 2022-02-18
F completed HH 2017-12-01 2018-12-18 2019-07-26 2022-02-18
H completed HH 2017-12-01 2018-12-18 2019-07-26 2022-02-18

我已将 df1 和 df2 与某些条件连接起来,代码如下:

df_last_month = df_last_month.set_index('project number')
df_current_month = df_current_month.set_index('project number')
df3 = pd.concat([df_last_month,df_current_month],sort=False)

df3a = df3.stack().groupby(level=[0,1]).unique().unstack(1).copy()

df3a.loc[~df3a.index.isin(df_last_month.index),'update_project'] = 'new'     

df3a.loc[~df3a.index.isin(df_current_month.index),'update_project'] ='deleted'

idx = df3.stack().groupby(level=[0,1]).nunique() 
df3a.loc[idx.mask(idx<=1).dropna().index.get_level_values(0),
'update_project'='modified'

df3a['update_project'] = df3a['update_project'].fillna('same')

这是输入:

我想要做的是:在列中(CF、CO、MA、VPC)我有两种格式:

  • 第一个:[2021-01-26 00:00:00]
  • 第二次:[2021-01-26 00:00:00,2021-01-10 00:00:00]
  1. 我想删除时间。

  2. 然后当我导出到 excel 时,我也会有相同的格式,我的意思是 [2021-01-26] 或 [2021-01-26,2021-01-10], 但现在我在 excel 中得到了这个结果:

这是我的代码:

import pandas as pd
import numpy as np
from datetime import datetime, date
# Classify date column by format type
df['format'] = 1
df.loc[df['CF'].astype(str).str.contains(','), 'format'] = 2
df['new_date'] = pd.to_datetime(df['CF'])

# Convert to datetime with two different format settings
df.loc[df.format == 1, 'new_date'] = pd.to_datetime(df.loc[df.format == 1, 'CF'], format = '%Y-%d-%m %H:%M:%S').dt.strftime('%Y-%m-%d')
df.loc[df.format == 2, 'new_date'] = pd.to_datetime(df.loc[df.format == 2, 'CF'], format = '%m/%d/%Y %H:%M:%S,%m/%d/%Y %H:%M:%S').dt.strftime('%Y-%m-%d,%m/%d/%Y')
print(df)

我有这个错误:

有什么建议吗?感谢您的帮助

【问题讨论】:

  • 你能提供一些文本格式的数据而不是图像吗?
  • 您的日期时间列似乎是一个列表[datetime1, datetime2]
  • 您的问题是您正在创建一个充满数组的数据框——您是如何得到它的?可以分享一下代码吗?
  • @Epsi95 我添加了我使用的数据
  • @Manakin 我添加第一个代码和数据

标签: python excel pandas date object


【解决方案1】:

如何在连接两个数据帧之前将日期时间列转换为字符串。这样你就可以得到你想要的输出。

from pandas.api.types import is_datetime64_any_dtype
    
for col in df_current_month.columns:
        if is_datetime64_any_dtype(df_current_month[col]):
            df_current_month[col] = df_current_month[col].dt.strftime('%Y-%m-%d')

df_current_month['CF'].head()
0    2021-10-01
1    2017-01-12
2    2017-01-12
3    2017-01-12
4    2017-01-12
Name: CF, dtype: object

不幸的是,您必须对两个数据帧都这样做。

【讨论】:

  • 是的,它正在工作!我将它应用于两个数据框,非常感谢:)
猜你喜欢
  • 1970-01-01
  • 2022-01-15
  • 2017-09-29
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-11-14
  • 1970-01-01
相关资源
最近更新 更多