Python | Панды tseries.offsets.DateOffset.nanos

Опубликовано: 27 Марта, 2022

Смещения даты - это стандартный вид приращения даты, используемый для диапазона дат в Pandas. Он работает точно так же, как relativedelta с точки зрения аргументов ключевого слова, которые мы передаем. DateOffets работает следующим образом, каждое смещение определяет набор дат, соответствующих DateOffset. Например, Bday определяет этот набор как набор дат, являющихся рабочими днями (MF).

DateOffsets могут быть созданы для перемещения дат вперед на заданное количество допустимых дат. Например, Bday (2) можно добавить к дате, чтобы переместить ее на два рабочих дня вперед. Если дата не начинается с действительной даты, сначала она перемещается на действительную дату, а затем создается смещение.

Pandas tseries.offsets.DateOffset.nanos attribute return the number of nano seconds in the time offset. It needs to be a fixed-frequency offset (something constant like a Day not a Business Day).

Syntax: pandas.tseries.offsets.DateOffset.nanos

Parameter : None

Returns : number of nanoseconds in the DateOffset

Example #1: Use pandas.tseries.offsets.DateOffset.nanos attribute to return the number of nanoseconds in the given DateOffset object.

# 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)

Выход :

Now we will add the dateoffset to the given timestamp object to roll forward the date from the given Date. Also return the number of nanoseconds in the given DateOffset object.

# 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.

Example #2: Use pandas.tseries.offsets.DateOffset.nanos attribute to return the number of nanoseconds in the given DateOffset object.

# 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)

Выход :

Now we will add the dateoffset to the given timestamp object to roll forward the date from the given Date. Also return the number of nanoseconds in the given DateOffset object.

# 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.

Внимание компьютерщик! Укрепите свои основы с помощью базового курса программирования Python и изучите основы.

Для начала подготовьтесь к собеседованию. Расширьте свои концепции структур данных с помощью курса Python DS. А чтобы начать свое путешествие по машинному обучению, присоединяйтесь к курсу Машинное обучение - базовый уровень.