简体   繁体   English

将 pandas 日期时间列转换为 Excel 序列日期

[英]Convert pandas datetime column to Excel serial date

I have a pandas dataframe with date values, however, I need to convert it from dates to text General format like in Excel, not to date string, in order to match with primary keys values in SQL, which are, unfortunately, reordered in general format. I have a pandas dataframe with date values, however, I need to convert it from dates to text General format like in Excel, not to date string, in order to match with primary keys values in SQL, which are, unfortunately, reordered in general格式。 Is it possible to do it Python or the only way to convert this column to general format in Excel?是否可以做到 Python 或将此列转换为 Excel 中的一般格式的唯一方法?

Here is how the dataframe's column looks like:以下是数据框列的样子:

   ID         Desired Output
1/1/2022        44562
7/21/2024       45494
1/1/1931        11324

Yes, it's possible.是的,这是可能的。 The general format in Excel starts counting the days from the date 1900-1-1. Excel 中的一般格式从日期 1900-1-1 开始计算天数。

You can calculate a time delta between the dates in ID and 1900-1-1.您可以计算 ID 和 1900-1-1 中的日期之间的时间差。

Inspired by this post you could do...这篇文章的启发,你可以做...

data = pd.DataFrame({'ID': ['1/1/2022','7/21/2024','1/1/1931']})
data['General format'] = (
    pd.to_datetime(data["ID"]) - pd.Timestamp("1900-01-01")
    ).dt.days + 2
print(data)
          ID  General format
0   1/1/2022           44562
1  7/21/2024           45494
2   1/1/1931           11324

The +2 is because: +2是因为:

  1. Excel starts counting from 1 instead of 0 Excel 从 1 而不是 0 开始计数
  2. Excel incorrectly considers 1900 as a leap year Excel 错误地将 1900 年视为闰年

First, determine the datatype.首先,确定数据类型。 Then, you will have something more to work with.然后,您将有更多的工作要做。 You could use '.astype()' to change the type of the data, an iterator to remove the '/' marks, or other methods to change it.您可以使用 '.astype()' 来更改数据的类型,使用迭代器来删除 '/' 标记,或使用其他方法来更改它。

Excel stores dates as sequential serial numbers so that they can be used in calculations. Excel 将日期存储为连续的序列号,以便可以在计算中使用它们。 By default, January 1, 1900 is serial number 1, and January 1, 2008 is serial number 39448 because it is 39,447 days after January 1, 1900.默认情况下,1900 年 1 月 1 日是序列号 1,而 2008 年 1 月 1 日是序列号 39448,因为它是 1900 年 1 月 1 日之后的 39,447 天。
-Microsoft's documentation -微软的文档

So you can just calculate (difference between your date and January 1, 1900) + 1所以你可以计算(difference between your date and January 1, 1900) + 1

see How to calculate number of days between two given dates请参阅如何计算两个给定日期之间的天数

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

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