Python 使用pandas更改列数据类型

Python 使用pandas更改列数据类型,python,excel,pandas,datetime,Python,Excel,Pandas,Datetime,我有一个带有日期列的数据框。列包括“自定义”和“常规”数据类型。我想更改所有日期时间格式。“43891”指“2020年3月1日00:00:00” 下面是我尝试过的,我也有同样的问题(参考) 当我应用这些代码时,我在下面共享输出。“一般类型”单元格变为“NaT” 在此解决方案中,所有列都是“常规”数据类型。正因为如此,问题才得以解决。但当我将上述代码应用于数据帧时,D列格式变成了“datetime”格式。因此,我在第二次运行代码时出现以下错误: TypeError: cannot astype a

我有一个带有日期列的数据框。列包括“自定义”和“常规”数据类型。我想更改所有日期时间格式。“43891”指“2020年3月1日00:00:00”

下面是我尝试过的,我也有同样的问题(参考)

当我应用这些代码时,我在下面共享输出。“一般类型”单元格变为“NaT”

在此解决方案中,所有列都是“常规”数据类型。正因为如此,问题才得以解决。但当我将上述代码应用于数据帧时,D列格式变成了“datetime”格式。因此,我在第二次运行代码时出现以下错误:

TypeError: cannot astype a datetimelike from [datetime64[ns]] to [int32]
我每天都会使用这些代码。因此,我需要解决格式单元格的问题。如果你愿意,我也可以尝试其他方法


我还有3000行。因此,我无法应用手动方法。

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

# zero_date = 1899-12-29
zero_date = pd.to_datetime('2020-03-01') - pd.to_timedelta(43891, unit='D')
然后您可以执行
np。选择

# 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')
                      )

用熊猫的方法来回答这个问题并没有答案。因此,我使用了“pynput.mouse”库

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

如果你有熊猫或任何其他方法,请回答

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
.
.

谢谢,它工作了,但是当我重新执行代码时,出现了这个错误:TypeError:dtype datetime64[ns]无法转换为timedelta64[ns]。这段代码每天都会运行,所以每天的零点日期都会改变。为了贡献这段代码,我每次都需要检查数字datetime。这是手动操作,您只能执行一次。之后,
df['TARIH']
已经是
datetime
类型,无法转换为
timedelta
。是的,我理解,但我需要可重复的代码。如果我在列中找到“整数值”,代码可能是可重复的。但我不知道怎么做。你说可重复是什么意思?您不想更改数据类型吗?重复性应从读取数据时开始。无论如何,尝试新代码,它应该重复运行。更新后,您不需要
general
获得最终输出。
# zero_date = 1899-12-29
zero_date = pd.to_datetime('2020-03-01') - pd.to_timedelta(43891, unit='D')
# 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')
                      )
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
.
.