python - 将日期更改为财政月份 pandas python

标签 python pandas

我希望使用 python pandas 将 df 中的日期更改为新列中的会计月份。

这是一个例子

17/01/2016    201601
18/01/2016    201602

你能帮我获取所需的Python代码吗?

最佳答案

你可以这样做:

In [29]: df['fiscal_month'] = np.where(df.Date.dt.day < 18, df.Date, df.Date + pd.DateOffset(months=1))

In [30]: df
Out[30]:
        Date        new fiscal_month
0 2016-01-17 2016-01-17   2016-01-17
1 2016-01-18 2016-02-01   2016-02-18

In [31]: df.fiscal_month = df.fiscal_month.dt.strftime('%Y%m')

In [32]: df
Out[32]:
        Date        new fiscal_month
0 2016-01-17 2016-01-17       201601
1 2016-01-18 2016-02-01       201602

关于python - 将日期更改为财政月份 pandas python,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40046935/

相关文章:

python - 在左连接中使用类似 SQL 的连接和过滤器(之间)合并表

python - Numpy uint8_t 数组到 vtkImageData

Python 与 Cpython

python - 我有一个单词的 df,我想知道它们是否在英语词典中

pandas - 在 Pandas 数据框中创建一个包含 bool 列组合计数的方阵

python - 从 Pandas 中的相关矩阵创建列表

python - qPython - kdb响应数据的类型转换

python - 如何使用其他表中已替换的 ID 加入查询?

python - 循环拉取文件大小、文件夹大小和目录大小?

python - GAE 数据存储缓存键与过滤器