📌  相关文章
📜  Python|熊猫 tseries.offsets.DateOffset.nanos

📅  最后修改于: 2022-05-13 01:54:31.079000             🧑  作者: Mango

Python|熊猫 tseries.offsets.DateOffset.nanos

Dateoffsets 是一种标准的日期增量,用于 Pandas 中的日期范围。就我们传入的关键字参数而言,它的工作方式与 relativedelta 完全相同。DateOffets 的工作方式如下,每个偏移量指定一组符合 DateOffset 的日期。例如, Bday将此集定义为工作日 (MF) 的日期集。

可以创建 DateOffsets 以将日期向前移动给定数量的有效日期。例如,可以将Bday(2)添加到日期以将其向前移动两个工作日。如果日期不是从有效日期开始的,则首先将其移至有效日期,然后创建偏移量。

Pandas tseries.offsets.DateOffset.nanos属性返回时间偏移的纳秒数。它需要是一个固定的频率偏移(像一天而不是工作日这样的常量)。

示例 #1:使用pandas.tseries.offsets.DateOffset.nanos属性返回给定 DateOffset 对象中的纳秒数。

# importing pandas as pd
import pandas as pd
  
# importing the to_offset function
from pandas.tseries.frequencies import to_offset
  
# Creating Timestamp
ts = pd.Timestamp('2019-10-10 07:15:11')
  
# Create the DateOffset of 2 day
do = to_offset(freq = '2D')
  
# Print the Timestamp
print(ts)
  
# Print the DateOffset
print(do)

输出 :

现在我们将 dateoffset 添加到给定的时间戳对象以从给定的日期前滚日期。还返回给定 DateOffset 对象中的纳秒数。

# Adding the dateoffset to the given timestamp
new_timestamp = ts + do
  
# Print the updated timestamp
print(new_timestamp)
  
# Now we will print the number of nanoseconds
# in the given DateOffset object
print(do.nanos)

输出 :

正如我们在输出中看到的,该属性已成功返回给定 Dateoffset 对象中的纳秒数。

示例 #2:使用pandas.tseries.offsets.DateOffset.nanos属性返回给定 DateOffset 对象中的纳秒数。

# importing pandas as pd
import pandas as pd
  
# importing the to_offset function
from pandas.tseries.frequencies import to_offset
  
# Creating Timestamp
ts = pd.Timestamp('2019-10-10 07:15:11')
  
# Create the DateOffset
do = to_offset(freq = '10D2H')
  
# Print the Timestamp
print(ts)
  
# Print the DateOffset
print(do)

输出 :

现在我们将 dateoffset 添加到给定的时间戳对象以从给定的日期前滚日期。还返回给定 DateOffset 对象中的纳秒数。

# Adding the dateoffset to the given timestamp
new_timestamp = ts + do
  
# Print the updated timestamp
print(new_timestamp)
  
# Now we will print the number of nanoseconds
# in the given DateOffset object
print(do.nanos)

输出 :

正如我们在输出中看到的,该属性已成功返回给定 Dateoffset 对象中的纳秒数。