Pandas 转换为日期时间

huangapple go评论58阅读模式
英文:

Pandas transform to datetime

问题

我已经收到一些以奇怪的格式给出的日期,我无法将它们解读为Pandas日期时间格式。

一个示例是:736698.0,它应该是'2017-01-04T00:00:00.000000000'。因此,原始格式似乎是自1/1/1BC以来的天数(作为零年的第一天,但没有这样的年份,所以是年份-1)。

我已经尝试使用pandas.to_datetime(736698.0, unit='D', origin=datetime.datetime(1/1/0))和其他组合,但我没有得到任何结果。

英文:

I've been given some dates in an strange format and I don't manage to read them as datetime pandas format.

An example would be: 736698.0, which should be '2017-01-04T00:00:00.000000000'. So, it seems the original format is days since 1/1/1BC (as first day of year zero, but there's no such year, so it's year -1).

I have tried to use pandas.to_datetime(736698.0, unit='D', origin=datetime.datetime(1/1/0)) and other combinations, but I'm not getting anything.

答案1

得分: 1

你可以使用以下方法将给定的浮点数转换为日期时间。由于无法设置year=0,我们必须从最终结果中减去它:

df['Date'] = df['Date_num'].apply(lambda x: datetime.datetime(1, 1, 1) + datetime.timedelta(x) - datetime.timedelta(366))

输出

Date_num Date

736698.0 2017-01-04

英文:

You can convert the given floats into a datetime with the following. As we cannot set a year=0, we must subtract that from the final result:

df['Date'] = df['Date_num'].apply(lambda x: datetime.datetime(1,1,1) + datetime.timedelta(x) - datetime.timedelta(366))

# Output
# Date_num	Date
# 736698.0	2017-01-04

答案2

得分: 1

以下是您要翻译的内容:

如果您想要使用矢量方法并且期望最终日期是最近的,您可以减去一个有效的纪元:

# 使用有效的起始日期
epoch = pd.to_datetime('1970-1-1')
# 定义此起始日期相对于您的参考日期的值
epoch_from_ref = 719527

# 执行转换
df['date'] = pd.to_datetime(df['col'].sub(epoch_from_ref),
                            unit='D', origin=epoch)

输出:

      col       date
0  736698 2017-01-05

使用的输入:

df = pd.DataFrame({'col': [736698]})
英文:

If you want a vectorial method and expect the final dates to be recent, you can subtract a valid epoch:

# use a valid origin
epoch = pd.to_datetime('1970-1-1')
# define the value of this origin relative to your reference
epoch_from_ref = 719527

# perform the conversion
df['date'] = pd.to_datetime(df['col'].sub(epoch_from_ref),
                            unit='D', origin=epoch)

Output:

      col       date
0  736698 2017-01-05

Used input:

df = pd.DataFrame({'col': [736698]})

答案3

得分: 1

使用 numpy 可以支持‘BC’年份的操作:

import numpy as np

>>> np.datetime64('000') + np.timedelta64(736698, 'D')
numpy.datetime64('2017-01-04')

示例:

import pandas as pd

df = pd.DataFrame({'Date': [736698.0]})

df['Date2'] = pd.to_timedelta(df['Date'], unit='D').to_numpy() + np.datetime64('000')

输出:

>>> df
       Date      Date2
0  736698.0 2017-01-04
英文:

Use numpy to do that as it supports 'BC' years:

import numpy as np

>>> np.datetime64('000') + np.timedelta64(736698, 'D')
numpy.datetime64('2017-01-04')

Example:

import pandas as pd

df = pd.DataFrame({'Date': [736698.0]})

df['Date2'] = pd.to_timedelta(df['Date'], unit='D').to_numpy() + np.datetime64('000')

Output:

>>> df
       Date      Date2
0  736698.0 2017-01-04

huangapple
  • 本文由 发表于 2023年6月6日 16:54:37
  • 转载请务必保留本文链接:https://go.coder-hub.com/76412952.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定