- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
这是当前 df:
ID Date
1 3/29/2017
2
3 11/5/2015
4
5 2/28/2017
我正在尝试将年+月作为新列中的字符串。这是我的代码:
df["Year"] = df["Date"].dt.year
df["Month"] = df["Date"].dt.month
df["yyyy_mm"] = df["Year"].map(str) + "-" + df["Month"].map(str)
问题是当我从日期中提取年份和月份时,它将返回 float 类型。
ID Date Year Month yyyy_mm I hope to get this
1 3/29/2017 2017.0 3.0 2017.0-3.0 2017-3
2 nan-nan
3 11/5/2015 2015.0 11.0 2015.0-11.0 2015-11
4 nan-nan
5 2/28/2017 2017.0 2.0 2017.0-2.0 2017-2
我试过用df["Date"].dt.year.astype(int)
把它转成int,这样就没有.0
了,但我收到此错误:无法将非有限值(NA 或 inf)转换为整数。因为列中有NAN。
我不想用 0 或其他东西填充所有年份和月份,我只想让它们为空,因为 date
在该行是空的。
最佳答案
您应该使用 pd.Series.dt.strftime
直接从 Date
执行字符串转换.
这不仅确保 NaT
行保持 NaT
,而且字符串的格式更好,例如几个月的零填充。
df["yyyy_mm"] = df['Date'].dt.strftime('%Y-%m')
print(df)
ID Date Year Month yyyy_mm
0 1 2017-03-29 2017.0 3.0 2017-03
1 2 NaT NaN NaN NaT
2 3 2015-11-05 2015.0 11.0 2015-11
3 4 NaT NaN NaN NaT
4 5 2017-02-28 2017.0 2.0 2017-02
关于python - 从具有 NA 值的 Date 获取 Year & Month 作为字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50160560/
我需要本地化所有可能性: 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(
我是一名优秀的程序员,十分优秀!