- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我有以下数据框:
我如何抵消 Pandas dayofyear 以便开始日期是 10 月 1 日而不是 1 月 1 日
在这种情况下,我希望年份是从 10 月 1 日到 9 月 30 日,并且需要考虑闰年。
下面是我想要输出的示例,其中日期列是唯一的变量。
这是表格形式的数据框:
Day stock dayofyear weekday month year leapyear
0 24/09/2019 10 267 1 9 2019 False
1 25/09/2019 10 268 2 9 2019 False
2 26/09/2019 11 269 3 9 2019 False
3 27/09/2019 12 270 4 9 2019 False
4 28/09/2019 14 271 5 9 2019 False
5 29/09/2019 14 272 6 9 2019 False
6 30/09/2019 15 273 0 9 2019 False
7 01/10/2019 16 274 1 10 2019 False
8 02/10/2019 17 275 2 10 2019 False
9 03/10/2019 18 276 3 10 2019 False
10 04/10/2019 19 277 4 10 2019 False
最佳答案
使用:
df['Day'] = pd.to_datetime(df['Day'], dayfirst=True)
base_year = np.where(df['month'].ge(10), df['year'], df['year'].sub(1))
base_date = pd.to_datetime(base_year, format='%Y') + pd.DateOffset(months=9)
df['dayofyear'] = (df['Day'] - base_date).dt.days.add(1)
详细信息:
使用 pd.to_datetime
将 Day
列转换为 pandas 日期时间系列,然后使用 np.where
连同 Series.gt
和 Series.sub
为 Day
列中的每个日期计算 base_year
。
print(base_year)
array([2018, 2018, 2018, 2018, 2018, 2018, 2018, 2019, 2019, 2019, 2019])
使用pd.to_datetime
将 base_year
转换为 pandas 日期时间系列并添加 9 个月
的偏移量,以便 base_date
从 10 月 1 日开始
.
print(base_date)
DatetimeIndex(['2018-10-01', '2018-10-01', '2018-10-01', '2018-10-01',
'2018-10-01', '2018-10-01', '2018-10-01', '2019-10-01',
'2019-10-01', '2019-10-01', '2019-10-01'],
dtype='datetime64[ns]', freq=None)
从这个 base_date
中减去 Day
列并使用 Series.dt.days
计算 dayofyear
:
print(df)
Day stock dayofyear weekday month year leapyear
0 2019-09-24 10 359 1 9 2019 False
1 2019-09-25 10 360 2 9 2019 False
2 2019-09-26 11 361 3 9 2019 False
3 2019-09-27 12 362 4 9 2019 False
4 2019-09-28 14 363 5 9 2019 False
5 2019-09-29 14 364 6 9 2019 False
6 2019-09-30 15 365 0 9 2019 False
7 2019-10-01 16 1 1 10 2019 False
8 2019-10-02 17 2 2 10 2019 False
9 2019-10-03 18 3 3 10 2019 False
10 2019-10-04 19 4 4 10 2019 False
关于python - 我如何抵消 Pandas dayofyear 以便开始日期是 10 月 1 日而不是 1 月 1 日?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63644970/
我正在使用 C#。 我知道我可以用 ToLongDateString() 显示如下内容: Friday, February 27, 2009 我喜欢做的是展示类似的东西: Febru
我将用户输入的时间和日期作为: DatePicker dp = (DatePicker) findViewById(R.id.datePicker); TimePicker tp = (TimePic
我需要检查是否 DateTime.Now是在每个月的前 3 个工作日(周一至周五)。我还需要提供一个 List有国定假日,应相应处理。 如果 DateTime.Now 是星期六并且是该月的 1 号,则
我想以 2012 年 1 月 1 日 - 2012 年 1 月 31 日的格式显示日期 并获取由 dateranges['jan 1,2012 - jan 31,2012','December 1,2
我有这两个函数可以以正确的格式 (mm-dd-yyyy) 创建一个新字符串,但现在它似乎工作得不太好......当我输入日期 31-03-2013 这是一个有效的日期,它出现在 04-01-2013
我只想在 Fullcalendar 中加载 December 月份,其中 2 个日期启用 2012 年 12 月 14 日、15 日。 var date = new Date(2012, 11); v
我创建了一个convertDate类,用于转换我的时间戳,即Date().sinceReferenceDate(即自2001年1月1日凌晨12:00起)。我有一个问题:当时间戳的日期超过 3 周前时,
我有来自两个来源的数据,我试图在日期/时间上进行匹配。问题是我没有得到匹配。所以我将日期/时间转换为 Julian 并遇到了一些奇怪的事情。 8/11/2015 6:50:22.000 42227
我正在尝试将毫秒转换为如下所示的日期:Oct 04, 2013 .我将毫秒转换为日期对象: var d1 = new Date(milliseconds); 然后输出如下内容: Fri Oct 04
我正在尝试使用 javascript 进行倒计时。但是,我的倒计时只能计算天数、小时数、分钟数和秒数。我还想显示年份和月份。 以下是我的代码: today = new Date();
我有下面的查询不适合我, 这个函数 from_tz(to_timestamp(START_TIME, 'YYYYMMDDHH24MISS'), substr(UTC_TIME_CODE_OFFSET,
例如,我有一列数据如下: 17.14.11 17.15.10 18.21.06 现在是 2017 年 11 月 14 日,我想将其更改为 DateTime 对象,例如: 2017-11-14 2017
$date1 = "2000-01-01"; $date2 = "2011-03-14"; $diff = abs(strtotime($date2) - strtotime($date1)); $y
我有以下代码来验证给定日期格式的日期: val df = new SimpleDateFormat("MM/dd/yyyy"); df.setLenient(false); try { val
我有一个包含两个 DateTime 成员的类。 但是,当我尝试将表示日期的“人类可读”值分配给 DateTime 成员时,它拒绝它并显示消息“字符串未被识别为有效的 DateTime”。 失败的代码行
已关闭。此问题不符合Stack Overflow guidelines 。目前不接受答案。 要求提供代码的问题必须表现出对所解决问题的最低限度的了解。包括尝试的解决方案、为什么它们不起作用以及预期结果
这个问题已经有答案了: Adding days to a date in Java [duplicate] (6 个回答) 已关闭 6 年前。 如何在 java 中将给定的日期转换为日历格式。 示例初
这个问题已经有答案了: Illegal pattern character 'T' when parsing a date string to java.util.Date (4 个回答) 已关闭 6
我现在在香港。如何获取美国的年、月、日? 我已经尝试过: Calendar calendar = Calendar.getInstance(); SimpleDateFormat sdf = new
我正在尝试使用 php 和 Mysql 开发一个搜索系统,但是您如何搜索:例如当数据库中的日期格式为 2012-03-26(年/月/日)时的三月份 最佳答案 根据列数据的格式,这样的事情可能会起作用:
我是一名优秀的程序员,十分优秀!