简体   繁体   English

Python日期时间增量格式

[英]Python datetime delta format

I am attempting to find records in my dataframe that are 30 days old or older.我试图在我的数据框中查找 30 天或更早的记录。 I pretty much have everything working but I need to correct the format of the Age column.我几乎一切正常,但我需要更正年龄列的格式。 Most everything in the program is stuff I found on stack overflow, but I can't figure out how to change the format of the delta that is returned.程序中的大部分内容都是我在堆栈溢出时找到的东西,但我不知道如何更改返回的增量的格式。

import pandas as pd
import datetime as dt

file_name = '/Aging_SRs.xls'
sheet = 'All'

df = pd.read_excel(io=file_name, sheet_name=sheet)

df.rename(columns={'SR Create Date': 'Create_Date', 'SR Number': 'SR'}, inplace=True)

tday = dt.date.today()
tdelta = dt.timedelta(days=30)
aged = tday - tdelta

df = df.loc[df.Create_Date <= aged, :]

# Sets the SR as the index.
df = df.set_index('SR', drop = True)

# Created the Age column.
df.insert(2, 'Age', 0)

# Calculates the days between the Create Date and Today.
df['Age'] = df['Create_Date'].subtract(tday)

The calculation in the last line above gives me the result, but it looks like -197 days +09:39:12 and I need it to just be a positive number 197 .上面最后一行的计算给出了结果,但它看起来像-197 days +09:39:12 ,我需要它只是一个正数197 I have also tried to search using the python, pandas, and datetime keywords.我还尝试使用 python、pandas 和 datetime 关键字进行搜索。

df.rename(columns={'Create_Date': 'SR Create Date'}, inplace=True)

writer = pd.ExcelWriter('output_test.xlsx')
df.to_excel(writer)
writer.save()

I can't see your example data, but IIUC and you're just trying to get the absolute value of the number of days of a timedelta, this should work:我看不到您的示例数据,但是 IIUC 和您只是想获得 timedelta 天数的绝对值,这应该有效:

df['Age'] =  abs(df['Create_Date'].subtract(tday)).dt.days)

Explanation :说明

Given a dataframe with a timedelta column:给定一个带有 timedelta 列的数据框:

>>> df
                 delta
0  26523 days 01:57:59
1 -1601 days +01:57:59

You can extract just the number of days as an int using dt.days :您可以使用dt.days将天数提取为int

>>> df['delta']dt.days
0    26523
1    -1601
Name: delta, dtype: int64

Then, all you need to do is wrap that in a call to abs to get the absolute value of that int:然后,您需要做的就是将其包装在对abs的调用中以获取该 int 的绝对值:

>>> abs(df.delta.dt.days)
0    26523
1     1601
Name: delta, dtype: int64

here is what i worked out for basically the same issue.这是我针对基本相同的问题所做的工作。

# create timestamp for today, normalize to 00:00:00
today = pd.to_datetime('today', ).normalize()
# match timezone with datetimes in df so subtraction works
today = today.tz_localize(df['posted'].dt.tz)
# create 'age' column for days old
df['age'] = (today - df['posted']).dt.days

pretty much the same as the answer above, but without the call to abs() .与上面的答案几乎相同,但没有调用abs()

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

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