简体   繁体   English

pandas 日期时间到 unix 时间戳秒

[英]pandas datetime to unix timestamp seconds

From the official documentation of pandas.to_datetime we can say,pandas.to_datetime的官方文档我们可以说,

unit : string, default ‘ns’

unit of the arg (D,s,ms,us,ns) denote the unit, which is an integer or float number. arg 的单位 (D,s,ms,us,ns) 表示单位,它是一个 integer 或浮点数。 This will be based off the origin.这将基于原点。 Example, with unit='ms' and origin='unix' (the default), this would calculate the number of milliseconds to the unix epoch start.例如,使用 unit='ms' 和 origin='unix'(默认值),这将计算到 unix 纪元开始的毫秒数。

So when I try like this way,所以当我这样尝试时,

import pandas as pd
df = pd.DataFrame({'time': [pd.to_datetime('2019-01-15 13:25:43')]})
df_unix_sec = pd.to_datetime(df['time'],unit='ms',origin='unix')
print(df)
print(df_unix_sec)

                 time
0   2019-01-15 13:25:43
0   2019-01-15 13:25:43
Name: time, dtype: datetime64[ns]

Output is not changing for the later one. Output 对于后一个没有变化。 Every time it is showing the datetime value not number of milliseconds to the unix epoch start for the 2nd one.每次它显示日期时间值而不是毫秒数到 unix 纪元开始为第二个。 Why is that?这是为什么? Am I missing something?我错过了什么吗?

I think you misunderstood what the argument is for.我想你误解了这个论点的目的。 The purpose of origin='unix' is to convert an integer timestamp to datetime , not the other way. origin='unix'的目的是将整数时间戳转换datetime ,而不是相反。

pd.to_datetime(1.547559e+09, unit='s', origin='unix') 
# Timestamp('2019-01-15 13:30:00')

Here are some options:以下是一些选项:

Option 1: integer division选项 1:整数除法

Conversely, you can get the timestamp by converting to integer (to get nanoseconds) and divide by 10 9 .相反,您可以通过转换为整数(以获取纳秒)并除以 10 9来获取时间戳。

pd.to_datetime(['2019-01-15 13:30:00']).astype(int) / 10**9
# Float64Index([1547559000.0], dtype='float64')

Pros:优点:

  • super fast超级快

Cons:缺点:

  • makes assumptions about how pandas internally stores dates对 pandas 如何在内部存储日期做出假设

Option 2: recommended by pandas选项2:熊猫推荐

Pandas docs recommend using the following method: Pandas 文档推荐使用以下方法:

# create test data
dates = pd.to_datetime(['2019-01-15 13:30:00'])

# calculate unix datetime
(dates - pd.Timestamp("1970-01-01")) // pd.Timedelta('1s')

[out]:
Int64Index([1547559000], dtype='int64')

Pros:优点:

  • "idiomatic", recommended by the library图书馆推荐的“惯用语”

Cons:缺点:

  • unweildy笨重
  • not as performant as integer division不如整数除法性能好

Option 3: pd.Timestamp选项 3: pd.Timestamp

If you have a single date string, you can use pd.Timestamp as shown in the other answer:如果您有单个日期字符串,则可以使用pd.Timestamp ,如另一个答案所示:

pd.Timestamp('2019-01-15 13:30:00').timestamp()
# 1547559000.0

If you have to cooerce multiple datetimes (where pd.to_datetime is your only option), you can initialize and map:如果您必须强制多个日期时间(其中pd.to_datetime是您唯一的选择),您可以初始化和映射:

pd.to_datetime(['2019-01-15 13:30:00']).map(pd.Timestamp.timestamp)
# Float64Index([1547559000.0], dtype='float64')

Pros:优点:

  • best method for a single datetime string单个日期时间字符串的最佳方法
  • easy to remember容易记住

Cons:缺点:

  • not as performant as integer division不如整数除法性能好

You can use timestamp() method which returns POSIX timestamp as float:您可以使用将 POSIX 时间戳作为浮点数返回的timestamp() 方法

pd.Timestamp('2021-04-01').timestamp()

[Out]:
1617235200.0

pd.Timestamp('2021-04-01 00:02:35.234').timestamp()

[Out]:
1617235355.234

value attribute of the pandas Timestamp holds the unix epoch. pandas Timestamp 的value属性包含 unix 纪元。 This value is in nanoseconds.该值以纳秒为单位。 So you can convert to ms or us by diving by 1e3 or 1e6.因此,您可以通过 1e3 或 1e6 转换为 ms 或 us。 Check the code below.检查下面的代码。

import pandas as pd
date_1 = pd.to_datetime('2020-07-18 18:50:00')
print(date_1.value) 

In case you are accessing a particular datetime64 object from the dataframe, chances are that pandas will return a Timestamp object which is essentially how pandas stores datetime64 objects.如果您从数据框中访问特定的datetime64对象,pandas 很可能会返回一个Timestamp对象,这本质上是 pandas 存储datetime64对象的方式。

You can use pd.Timestamp.to_datetime64() method of the pd.Timestamp object to convert it to numpy.datetime64 object with ns precision.您可以使用pd.Timestamp对象的pd.Timestamp.to_datetime64()方法将其转换为具有ns精度的numpy.datetime64对象。

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

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