简体   繁体   English

将 Month&Year 列转换为日期时间格式 python

[英]Convert Month&Year column into datetime format python

I have some excel that has the columns Month and Year and I want to have a newly created column that converts the month and year into a datetime column of the format %d/%m/%Y with the day being the 1st of that month.我有一些 excel,其中包含 Month 和 Year 列,我想要一个新创建的列,将月份和年份转换为格式为 %d/%m/%Y 的日期时间列,其中一天是该月的 1 号.

Example:例子:

Month Year
3 3 2021 2021年
5 5 2021 2021年

The new column should look like: 01-03-2021.新列应如下所示:01-03-2021。

Datum基准
01/03/2021 01/03/2021
01/05/2021 01/05/2021

I have tried this:我试过这个:

import datetime
df = pd.read_excel(r"C:\Users\f0cdnu\Downloads\Test.xlsx")
df['Datum'] = datetime.datetime(df.Year, df.Month,1)
df

Gives cant convert series to int error and this:给出cant convert series to int 错误和这个:

df = pd.read_excel(r"C:\Users\f0cdnu\Downloads\Test.xlsx")
df['Datum'] = pd.to_datetime(df.Year*10000+df.Month*100)

gives毫米

A datetime in both Python and Excel has no format. Python 和 Excel 中的datetime时间都没有格式。 In both cases it's a binary value.在这两种情况下,它都是一个二进制值。 In Excel dates are stored as OLE Automation DATE values, also known as OADAte - a 64-bit float where the integral part is an offset from Decemberr 30, 1899 and the fractional part the time of day.在 Excel 中,日期存储为OLE 自动化 DATE值,也称为 OADAte - 一个 64 位浮点数,其中整数部分是 1899 年 12 月 30 日的偏移量,小数部分是一天中的时间。

Formats apply only when parsing text into dates or formating dates to strings for export or display.格式仅在将文本解析为日期或将日期格式化为字符串以进行导出或显示时适用。 In Excel, the display of a date depends on the cell's style.在 Excel 中,日期的显示取决于单元格的样式。

Since you use Pandas, the problem becomes:既然你使用 Pandas,问题就变成了:

  1. how to create a new datetime column from parts and如何从零件创建新的datetime列和
  2. how to control the cell style when exporting to Excel导出到Excel时如何控制单元格样式

Create the column创建列

The answers to How to convert columns into one datetime column in pandas? 如何在 Pandas 中将列转换为一个日期时间列的答案 show several ways to add the new column.显示几种添加新列的方法。 The most elegant would be :最优雅的是:

df['Datum'] = pd.to_datetime(dict(year=df.Year, month=df.Month, day=1))

or要么

df['Datum'] =pd.to_datetime(df.Year*10000+df.Month*100+1,format='%Y%m%d')

In this case the number is treated as string parsed using the format parameter.在这种情况下,数字被视为使用format参数解析的字符串。

Specify a date format in Excel在 Excel 中指定日期格式

The answers to Python Pandas custom time format in Excel output show how to control the display style of datetime columns through the datetime_format property of the ExcelWriter object : Excel 输出中 Python Pandas 自定义时间格式的答案展示了如何通过ExcelWriter对象的datetime_format属性控制datetime列的显示样式:

writer = pd.ExcelWriter("time.xlsx",  datetime_format='dd/mm/yyyy')
df.to_excel(writer, "Sheet1")

Pandas uses XlsxWriter to write to Excel. Pandas 使用XlsxWriter写入 Excel。 Working with Dates and Time how XlsxWriter works with dates in general and Working with Python Pandas and XlsxWriter how to work with Pandas and how to control formatting. 使用日期和时间XlsxWriter 如何使用一般日期以及使用 Python Pandas 和 XlsxWriter如何使用 Pandas 以及如何控制格式。

For example, you can set the default date and time styles for all cells:例如,您可以为所有单元格设置默认日期和时间样式:

writer = pd.ExcelWriter("pandas_datetime.xlsx",
                        engine='xlsxwriter',
                        datetime_format='mmm d yyyy hh:mm:ss',
                        date_format='mmmm dd yyyy')

Or you can specify formats for specific ranges :或者您可以为特定范围指定格式:

# Add some cell formats.
format1 = workbook.add_format({'num_format': '#,##0.00'})
format2 = workbook.add_format({'num_format': '0%'})

# Set the column width and format.
worksheet.set_column('B:B', 18, format1)

# Set the format but not the column width.
worksheet.set_column('C:C', None, format2)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM