Python Pandas-根据其他列的日期建立日期
我正在尝试根据数据框架中不同列的日期来构建日期列。它归结为我公司的财政日历在每个月的22日重置。如果我的源列中的日期是本月22日之前,那么我的日期就必须是22日。如果是在第22个之后,则需要在下个月的22日。
我构建了一些代码,但是我无法正确运行它,我遇到了一个错误系列的真实价值是模棱两可的。 ),A.Any()或A.all()。
使用A.Empty,A.Bool(),A.Item ( 列,然后将它们加入日期,然后删除我用于处理的所有列。
这是当前代码的方向:
df_connection['Year_OLD'] = df_connection['Date Created'].dt.year
df_connection['Mon_OLD'] = df_connection['Date Created'].dt.month
df_connection['Day_OLD'] = df_connection['Date Created'].dt.day
def func_Year(y, m, d):
if (m == 12) & (d >= 22):
return y + 1
else:
return y
def func_Mon(y, m, d):
if (m < 12) & (d >=22):
return m + 1
elif (m == 12) & (d >= 22):
return 1
else:
return m
df_connection['Year'] = func_Year(df_connection['Year_OLD'], df_connection['Mon_OLD'], df_connection['Day_OLD'])
I'm attempting to build a column of dates, based on dates from a different column in a dataframe. It boils down to my company fiscal calendar resetting on the 22nd of every month. If a date in my source column is before the 22nd of the month, then my date needs to be the 22nd. If it's after the 22nd it needs to be on the 22nd of the following month.
I built some code, but I can't get it functioning correctly, I get an error The truth value of a Series is ambiguous. Use a.empty, a.bool(), a.item(), a.any() or a.all().
I was thinking I'd break the old date apart into separate columns, process each column, then join them back up into a date and remove all the columns I'd use to process.
Here is the direction of the current code:
df_connection['Year_OLD'] = df_connection['Date Created'].dt.year
df_connection['Mon_OLD'] = df_connection['Date Created'].dt.month
df_connection['Day_OLD'] = df_connection['Date Created'].dt.day
def func_Year(y, m, d):
if (m == 12) & (d >= 22):
return y + 1
else:
return y
def func_Mon(y, m, d):
if (m < 12) & (d >=22):
return m + 1
elif (m == 12) & (d >= 22):
return 1
else:
return m
df_connection['Year'] = func_Year(df_connection['Year_OLD'], df_connection['Mon_OLD'], df_connection['Day_OLD'])
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
iiuc,您可以使用检查特定的一天是更大还是等于22。如果是的,请使用
dateOffset
添加一个月。之后全天用22代替。IIUC, you can use
mask
to check if a specific day is greater or equal 22. If so, useDateOffset
to add one month. Afterwards replace all days with 22.在您的代码中,您将数据框架直接传递给不正确的函数
In your code you are passing dataframe directly to the function which is not correct