- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
以下序列是我得到的 pandas DataFrame 的摘录:
>>> df_t
value
2011-01-31 -5.575000
2011-03-31 7.700000
2011-05-31 15.966667
2011-07-31 10.683333
2011-08-31 10.454167
2011-10-31 9.320833
2011-12-31 -0.358333
2012-01-31 -11.550000
2012-03-31 1.700000
2012-05-31 12.333333
2012-07-31 12.816667
2012-08-31 11.837500
2012-10-31 2.733333
2012-12-31 4.075000
2013-01-31 2.450000
2013-03-31 -4.262500
2013-05-31 11.491667
2013-07-31 14.812500
2013-08-31 13.920833
2013-10-31 4.125000
2013-12-31 0.075000
如何删除闰年的3月31日?我试过类似的东西:
def isleap(year):
return year % 4 == 0 and (year % 100 != 0 or year % 400 == 0)
if isleap(df_t.index.year):
df_t=df_t[df_t.index.dayofyear!=91]
...但显然,这在我的脑海中过于直截了当。如果年份是闰年并且日期是一年中的第 91 天,那么循环遍历整个数据框并在每一步检查的唯一解决方案是吗?还是有更简单的解决方案可用?
编辑:问题不在于如何确定一年是否为闰年,而是如果是,则删除上述数据框中的 3 月 31 日。
最佳答案
这是一个以矢量化方式执行此操作的示例。您应该注意到 and
和 or
不适用于 bool 向量,请改用 &
和 |
。
import pandas as pd
import numpy as np
s = pd.Series(np.random.randn(600), index=pd.date_range('1990-01-01', periods=600, freq='M'))
Out[76]:
1990-01-31 -0.7594
1990-02-28 -0.1311
1990-03-31 1.2031
1990-04-30 1.1999
1990-05-31 -2.4399
...
2039-08-31 -0.3554
2039-09-30 -0.3265
2039-10-31 -0.3832
2039-11-30 -1.4139
2039-12-31 -0.3086
Freq: M, dtype: float64
def is_leap_and_MarchEnd(s):
return (s.index.year % 4 == 0) & ((s.index.year % 100 != 0) | (s.index.year % 400 == 0)) & (s.index.month == 3) & (s.index.day == 31)
mask = is_leap_and_MarchEnd(s)
s[mask]
Out[77]:
1992-03-31 0.7834
1996-03-31 0.3121
2000-03-31 -1.2050
2004-03-31 0.6017
2008-03-31 0.1045
...
2020-03-31 1.1037
2024-03-31 0.5139
2028-03-31 -0.8116
2032-03-31 -0.6939
2036-03-31 -1.1999
dtype: float64
# do delete these row
s[~mask]
关于python - Pandas 数据帧 : Delete specific date in all leap years,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30997007/
我需要本地化所有可能性: 1年, 2 年,然后……(不超过任何数字)。 1个月, 2 个月,然后……(不超过任何数字)。 在各种语言中,“年”这个词会根据它前面的数字而变化。比如法语: 22 年 =
本文整理了Java中org.joda.time.Years.years()方法的一些代码示例,展示了Years.years()的具体用法。这些代码示例主要来源于Github/Stackoverflow
年.hs: year = year + 1 main = print year 这不是尾递归调用: year = year + 1 year = (year + 1) + 1 year = ((yea
我有一个表,其中包含来自流量计的数据,排列如下: Water.Year May Jun Jul Aug Sep Oct Nov Dec Jan Feb
本文整理了Java中com.meidusa.amoeba.sqljep.function.Year.year()方法的一些代码示例,展示了Year.year()的具体用法。这些代码示例主要来源于Git
我是 Java 新手,希望获得一些有关正在尝试解决的问题的建议。我必须编写一个方法来生成唯一的编号。 因此,以字符串表示的唯一数字将有 10 个字符。 #1. First 2 will represe
谁有权威的答案,哪个性能更好? GROUP BY YEAR(FROM_UNIXTIME(col)) /* or */ GROUP BY EXTRACT(YEAR FROM FROM_U
我从今年过去了 20 年。但目前我希望它显示为年复一年。例如:1992 - 1999 1999 - 2002 2003 - 2015 这是我的代码。 function populatedropdown
我目前能够将 Landsat 图像拆分为 LC80440142014093LGN00.tar.gz 的日期信息, 我把它分成 2014093 .现在我需要将其更改为 Excel 或 Open Offi
我有一些数据想总结一下。我想对所有列进行总结,固定 YEAR 列。即对于一个变量我可以做: df %>% group_by(LG1, YEAR) %>% summarise(Freq = n(
我有一个独特的问题,我根本无法解决。 所以我在 SQL Server 2005 中,我得到了以下要处理的数据: FISCAL_YEAR_START_MONTH INT(财政年度的第一个月) COUNT
我有一些数据想总结一下。我想对所有列进行总结,固定 YEAR 列。即对于一个变量我可以做: df %>% group_by(LG1, YEAR) %>% summarise(Freq = n(
我想从指定年份的表中获取所有数据。但问题是我将日期设置为 DATETIME 类型。 我正在尝试类似的东西: SELECT * FROM table WHERE entrytim
使用日期选择器,年份下拉菜单默认只显示 10 年。用户必须单击最后一年才能添加更多年份。 我们如何将初始范围设置为 100 年,以便用户默认看到一个大列表? function InitDate
我必须将我的数据帧从当前格式转换为新格式(请参阅下面的图像或结构)。我不知道如何才能做到这一点。我想要每个 ID 一年,从 2013 年到 2018 年(所以每个 ID 有 6 行,每年一个)。日期是
在学习了 Hadley 的书并在 SO 上搜索之后,我创建了一个由年和月矩阵组成的热图,颜色根据时间序列变量的百分比变化而变化。热图和我用来获取它的代码如下所示。我还有一些我自己无法解决的问题。 1)
在电子表格上,我有各种谷歌分析 channel (有机、直接、移动、桌面等)的月度报告表。对于每个 channel ,我都有另一列自动计算 Y-o-Y EX 的增长百分比:(A12-A1)/A1。然后
我有几个气象变量的时间序列数据集。时间数据记录在三个单独的列中: 年份(例如 2012) 一年中的第几天(例如 261 代表闰年的 9 月 17 日) 时:分(例如 1610) 有没有一种方法可以合并
YEAR(x) 和 EXTRACT(YEAR FROM x) 在 MySQL 中是否等价? 似乎在我所有的实验中都有,但我不确定是否没有任何边缘情况。 举个例子:这两个查询返回完全相同的结果。但我不确
我读到“将日期字符串转换为mysql日期时间字段”:但这对我没有帮助 所以我想做的是将dat文件读入mysql中的空表。 table CLIMBED(TRIP_ID int,PEAK VARCHAR(
我是一名优秀的程序员,十分优秀!