【问题标题】:changing column data type using pandas使用 pandas 更改列数据类型
【发布时间】:2020-07-24 06:13:22
【问题描述】:

我有一个带有日期列的数据框。列包括“自定义”和“通用”数据类型。我想改变它所有的日期时间格式。 “43891”表示“01.03.2020 00:00:00”

TARİH
28.02.2020  00:00:00 -->custom
28.02.2020  00:00:00 -->custom
28.02.2020  00:00:00 -->custom
43891 -->general
43891 -->general
43891 -->general
.
.

这是我在下面尝试过的,我遇到了同样的问题(参考changing all dates to standard date time in dataframe

import pandas as pd
from datetime import datetime, timedelta

def from_excel_ordinal(ordinal, _epoch0=datetime(1899, 12, 31)):
    if ordinal >= 60:
        ordinal -= 1  # Excel leap year bug, 1900 is not a leap year!
    return (_epoch0 + timedelta(days=ordinal)).replace(microsecond=0)

df = pd.read_excel('D:\Documents\Desktop\deneme/deneme1.xlsx', sheet_name='Sheet1')
m = df['TARİH'].astype(str).str.isdigit()

df.loc[m, 'TARİH'] = \
df.loc[m, 'TARİH']\
  .astype(int)\
  .apply(from_excel_ordinal)

df['TARİH'] = pd.to_datetime(df['TARİH'], errors='coerce')

df.to_excel('D:\Documents\Desktop\deneme/deneme1.xlsx',index=False)

当我应用这些代码时,我将在下面分享输出。 “常规类型”单元格变为“NaT”。

print(df.loc[3280:3286, 'TARİH'])

Output: 
2020-02-28
2020-02-28
2020-02-28
2020-02-28
NaT
NaT
NaT
Name: TARİH, dtype: datetime64[ns]

在此解决方案中,changing all dates to standard date time in dataframe 所有列都是“通用”数据类型。由于这个问题得到解决。但是当我将上述代码应用于我的数据框时,D 列格式正在变成“日期时间”格式。由于第二次运行代码时出现以下错误:

TypeError: cannot astype a datetimelike from [datetime64[ns]] to [int32]

我每天都会使用这些代码。因此,我需要解决格式单元格问题。如果您愿意,我也可以尝试其他方法。

我还有 3000 行。所以我无法应用手动方法。

【问题讨论】:

    标签: python excel pandas datetime


    【解决方案1】:

    IIUC,43891 是自零日期以来的天数:

    # zero_date = 1899-12-29
    zero_date = pd.to_datetime('2020-03-01') - pd.to_timedelta(43891, unit='D')
    

    然后你就可以np.select:

    # you need dayfist
    custom = pd.to_datetime(df['TARİH'], dayfirst=True, errors='coerce')
    
    # general type
    df['TARİH'] = np.where(custom.isna(), df['TARİH'],
                           (custom - zero_date)/pd.to_timedelta('1D')
                          )
    

    【讨论】:

    • 感谢它起作用了,但是当我重新执行代码时,出现了这个错误:TypeError: dtype datetime64[ns] cannot be convert to timedelta64[ns]。此代码将每天运行,因此每天 zero_date 都会更改。要贡献此代码,我需要每次都检查数字日期时间。这是手工工作
    • 只能执行一次。之后df['TARIH']已经是datetime类型,不能转换为timedelta
    • 是的,我明白,但我需要可重复的代码。也许如果我在列中找到“整数值”,代码可能是可重复的。但我不知道该怎么做。
    • 可重复是什么意思?您不想更改数据类型吗?可重复性应该从您读取数据时开始。不管怎样,试试新代码,它应该重复运行
    • 已更新,最终输出不需要general
    【解决方案2】:

    pandas 方法没有答案。因此,我使用了“pynput.mouse”库。

    当您使用鼠标控制器方法将列样式更改为“短日期”时,df['TARİH'] = pd.to_datetime(df['TARİH']) 此代码运行由于那里没有混合的日期时间和整数传递数组,整列具有相同的格式。

    如果你有pandas或者其他方法,请回答。

    from pynput.mouse import Button, Controller
    import pandas as pd
    
    #Go to desktop
    mouse= Controller ()
    mouse.move(1358,751)
    mouse.click(Button.left, 1)
    
    #Open folder
    mouse.position=(632, 108)
    time.sleep(2)
    mouse.click(Button.left,2)
    
    #Open excel file
    mouse.position=(354, 127)
    time.sleep(2)
    mouse.click(Button.left,2)
    
    #Select D column in excel
    mouse.position=(250, 256)
    time.sleep(10)
    mouse.click(Button.left,1)
    
    #Go to format cell area
    mouse.position=(709, 87)
    time.sleep(2)
    mouse.click(Button.left,1)
    
    #Change format to short date
    mouse.position=(663, 297)
    time.sleep(2)
    mouse.click(Button.left,1)
    
    #Close excel file
    mouse.position=(1337, 11)
    time.sleep(2)
    mouse.click(Button.left,1)
    
    #Save excel file
    mouse.position=(597, 400)
    time.sleep(2)
    mouse.click(Button.left,1)
    
    #wait till excel close
    time.sleep(3)
    
    print("Formula writing operation is starting..")
    df = pd.read_excel('D:\Documents\Desktop\deneme/2020 Data_çalışma.xlsx', sheet_name='Sheet1')
    df['TARİH'] = pd.to_datetime(df['TARİH'])
    print("Formula is written..")
    
    
    Output:
    TARİH
    28.02.2020  00:00:00
    28.02.2020  00:00:00
    28.02.2020  00:00:00
    01.03.2020  00:00:00
    01.03.2020  00:00:00
    01.03.2020  00:00:00
    .
    .
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-06-18
      • 1970-01-01
      • 2018-05-25
      • 1970-01-01
      • 2019-11-08
      • 2023-01-21
      • 1970-01-01
      • 2020-05-12
      相关资源
      最近更新 更多