我正在尝试 return 只是截止日期和发布日期之间的天数。然而,它也是 return 时分秒
I am trying to return just the amount of days between deadline and launch date. However it is returning the hours minutes and seconds as well
这是我目前的代码:
import pandas as pd
df = pd.read_csv('/content/drive/MyDrive/Colab Datasets/KickstarterRevised.csv')
df['deadline'] = pd.to_datetime(df['deadline'])
df['launched'] = pd.to_datetime(df['launched'])
df['difference'] = df['deadline'].sub(df['launched'], axis=0)
df['difference']
0 58 days 23:24:00
1 45 days 00:00:00
2 30 days 01:00:00
3 55 days 16:25:00
4 35 days 00:00:00
...
4994 40 days 00:00:00
4995 8 days 10:50:00
4996 38 days 18:53:00
4997 30 days 00:00:00
4998 30 days 00:00:00
Name: difference, Length: 4999, dtype: timedelta64[ns]
正如您从代码中看到的,df['difference']
是一个带有 dtype: timedelta64[ns]
的系列。要获取天数,只需使用.astype("timedelta64[D]")
,见下文
df['difference'] = df['deadline'].sub(df['launched'], axis=0).astype('timedelta64[D]')
这是我目前的代码:
import pandas as pd
df = pd.read_csv('/content/drive/MyDrive/Colab Datasets/KickstarterRevised.csv')
df['deadline'] = pd.to_datetime(df['deadline'])
df['launched'] = pd.to_datetime(df['launched'])
df['difference'] = df['deadline'].sub(df['launched'], axis=0)
df['difference']
0 58 days 23:24:00
1 45 days 00:00:00
2 30 days 01:00:00
3 55 days 16:25:00
4 35 days 00:00:00
...
4994 40 days 00:00:00
4995 8 days 10:50:00
4996 38 days 18:53:00
4997 30 days 00:00:00
4998 30 days 00:00:00
Name: difference, Length: 4999, dtype: timedelta64[ns]
正如您从代码中看到的,df['difference']
是一个带有 dtype: timedelta64[ns]
的系列。要获取天数,只需使用.astype("timedelta64[D]")
,见下文
df['difference'] = df['deadline'].sub(df['launched'], axis=0).astype('timedelta64[D]')