Python Pandas-根据其他列的日期建立日期

发布于 2025-01-24 03:56:56 字数 892 浏览 2 评论 0原文

我正在尝试根据数据框架中不同列的日期来构建日期列。它归结为我公司的财政日历在每个月的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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

如梦 2025-01-31 03:56:56

iiuc,您可以使用检查特定的一天是更大还是等于22。如果是的,请使用 dateOffset 添加一个月。之后全天用22代替。

# create example data
daterange = pd.DataFrame(pd.date_range('2022-01-01', '2022-12-31'), columns=['Date Created']

daterange['Date Created'].mask(
    daterange['Date Created'].dt.day >= 22,
    daterange['Date Created'] + pd.DateOffset(months=1)
).apply(lambda x: x.replace(day=22))

IIUC, you can use mask to check if a specific day is greater or equal 22. If so, use DateOffset to add one month. Afterwards replace all days with 22.

# create example data
daterange = pd.DataFrame(pd.date_range('2022-01-01', '2022-12-31'), columns=['Date Created']

daterange['Date Created'].mask(
    daterange['Date Created'].dt.day >= 22,
    daterange['Date Created'] + pd.DateOffset(months=1)
).apply(lambda x: x.replace(day=22))
你的呼吸 2025-01-31 03:56:56

在您的代码中,您将数据框架直接传递给不正确的函数

您可以使用LAMDA函数

一个接一个地传递值

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'] = df_connection.apply(lambda x: func_Year(x['Year_OLD'], x['Mon_OLD'], x['Day_OLD']))

In your code you are passing dataframe directly to the function which is not correct

You can pass values one by one by using lamda function

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'] = df_connection.apply(lambda x: func_Year(x['Year_OLD'], x['Mon_OLD'], x['Day_OLD']))
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文