简体   繁体   English

Python Pandas 将 10 位日期时间转换为正确的日期格式

[英]Python Pandas Convert 10 digit datetime to a proper date format

I have an excel file which contains date format in 10 digit.我有一个 excel 文件,其中包含 10 位数字的日期格式。

For example, Order Date as 1806825282.731065, Purchase Date as 1806765295例如,订单日期为 1806825282.731065,购买日期为 1806765295

Does anyone know how to convert them to a proper date format such as dd/mm/yyyy hh:mm or dd/mm/yyyy ?有谁知道如何将它们转换为正确的日期格式,例如dd/mm/yyyy hh:mmdd/mm/yyyy Any date format will be fine.任何日期格式都可以。

I tried pd.to_datetime but does not work.我试过pd.to_datetime但没有用。

Thanks!谢谢!

SAS timestamp are stored in seconds from 1960-1-1: SAS 时间戳从 1960-1-1 开始以秒为单位存储:

import pandas as pd

origin = pd.Timestamp('1960-1-1')
df = pd.DataFrame({'Order Date': [1806825282.731065],
                   'Purchase Date': [1806765295]})

df['Order Date'] = origin + pd.to_timedelta(df['Order Date'], unit='s')
df['Purchase Date'] = origin + pd.to_timedelta(df['Purchase Date'], unit='s')

Output: Output:

>>> df
                     Order Date       Purchase Date
0 2017-04-03 07:54:42.731065035 2017-04-02 15:14:55

From The Essential Guide to SAS Dates and Times基本指南到 SAS 日期和时间

SAS has three separate counters that keep track of dates and times. SAS 有三个独立的计数器来跟踪日期和时间。 The date counter started at zero on January 1, 1960. Any day before 1/1/1960 is a negative number, and any day after that is a positive number.日期计数器从 1960 年 1 月 1 日的零开始。1/1/1960 之前的任何一天都是负数,之后的任何一天都是正数。 Every day at midnight, the date counter is increased by one.每天午夜,日期计数器都会加一。 The time counter runs from zero (at midnight) to 86,399.9999, when it resets to zero.时间计数器从零(午夜)运行到 86,399.9999,然后重置为零。 The last counter is the datetime counter.最后一个计数器是日期时间计数器。 This is the number of seconds since midnight, January 1, 1960. Why January 1, 1960?这是自 1960 年 1 月 1 日午夜以来的秒数。为什么是 1960 年 1 月 1 日? One story has it that the founders of SAS wanted to use the approximate birth date of the IBM 370 system, and they chose January 1, 1960 as an easy- to-remember approximation.有一个故事说,SAS 的创始人想使用 IBM 370 系统的大概诞生日期,他们选择了 1960 年 1 月 1 日作为一个易于记忆的近似值。

You can do this你可以这样做

(pd.to_timedelta(1806825282, unit='s') + pd.to_datetime('1960-1-1'))

or或者

(pd.to_timedelta(df['Order Date'], unit='s') + pd.to_datetime('1960-1-1'))

You can use something like this:你可以使用这样的东西:

# Convert the 10-digit datetime to a datetime object
df['date_column'] = pd.to_datetime(df['date_column'], unit='s')

# Format the datetime object to the desired format
df['date_column'] = df['date_column'].dt.strftime('%d/%m/%Y %H:%M')

Or if you want a one-liner:或者,如果您想要单线:

df['date_column'] = pd.to_datetime(df['date_column'], unit='s').dt.strftime('%d/%m/%Y %H:%M')

According to The Pandas Documentation Link: https://pandas.pydata.org/docs/reference/api/pandas.to_datetime.html根据 Pandas 文档链接: https://pandas.pydata.org/docs/reference/api/pandas.to_datetime.html

Code代码

>>> pd.to_datetime(1674518400, unit='s')
Timestamp('2023-01-24 15:16:45')

>>> pd.to_datetime(1674518400433502912, unit='ns')
Timestamp('2023-01-24 15:16:45.433502912')

# you can use template
df[DATE_FIELD]=(pd.to_datetime(df[DATE_FIELD],unit='ms')) 

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

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