如何在polars中从日期计算月初和月末日期?

huangapple go评论66阅读模式
英文:

How to calculate the month begin and end month date from date in polars?

问题

有没有一种有效的方法可以在日期列上获取月末日期。比如,如果日期=‘2023-02-13”,返回“2023-02-28”,同时返回月初也很好。谢谢!

df = pl.DataFrame({'DateColumn': ['2022-02-13']})

test_df = df.with_columns([
    pl.col('DateColumn').str.strptime(pl.Date).cast(pl.Date)
])

完美的话,可以得到两个新列。

英文:

Is there an efficient way to get the month end date on a date column. Like if date =‘2023-02-13” to return “2023-02-28”, also beginning of the month would be great as well.
Thanks!

df = pl.DataFrame({'DateColumn': ['2022-02-13']})

test_df = df.with_columns([
    pl.col('DateColumn').str.strptime(pl.Date).cast(pl.Date)
]
)

┌────────────┐
│ DateColumn │
│ ---        │
│ date       │
╞════════════╡
│ 2022-02-13 │
└────────────┘

Two new columns would be perfect.

答案1

得分: 6

[Update]: Polars自那时已添加了.month_start().month_end()方法。

请参见@n-maks的回答:答案链接

你可以使用.truncate

.offset_by

test_df.with_columns(
   MonthStart = pl.col("DateColumn").dt.truncate("1mo"),
   MonthEnd = pl.col("DateColumn").dt.offset_by("1mo").dt.truncate("1mo").dt.offset_by("-1d")
)
shape: (1, 3)
┌────────────┬────────────┬────────────┐
 DateColumn | MonthStart | MonthEnd   
 ---        | ---        | ---        
 date       | date       | date       
╞════════════╪════════════╪════════════╡
 2022-02-13 | 2022-02-01 | 2022-02-28 
└────────────┴────────────┴────────────┘
英文:

[Update]: Polars has since added .month_start() and .month_end() methods.

See the answer from @n-maks


You could use .truncate
and
.offset_by

test_df.with_columns(
   MonthStart = pl.col("DateColumn").dt.truncate("1mo"),
   MonthEnd = pl.col("DateColumn").dt.offset_by("1mo").dt.truncate("1mo").dt.offset_by("-1d")
)
shape: (1, 3)
┌────────────┬────────────┬────────────┐
│ DateColumn | MonthStart | MonthEnd   │
│ ---        | ---        | ---        │
│ date       | date       | date       │
╞════════════╪════════════╪════════════╡
│ 2022-02-13 | 2022-02-01 | 2022-02-28 │
└────────────┴────────────┴────────────┘

答案2

得分: 1

以下是代码部分的翻译:

df = pl.DataFrame({'DateColumn': ['2022-02-13']})

test_df = df.with_columns([
    pl.col('DateColumn').str.strptime(pl.Date).cast(pl.Date)
])

test_df.with_columns(
   pl.col('DateColumn').dt.month_start().alias('MonthStart'),
   pl.col('DateColumn').dt.month_end().alias('MonthEnd')
)
shape: (1, 3)
┌────────────┬────────────┬────────────┐
 DateColumn  MonthStart  MonthEnd   
 ---         ---         ---        
 date        date        date       
╞════════════╪════════════╪════════════╡
 2022-02-13  2022-02-01  2022-02-28 
└────────────┴────────────┴────────────┘
英文:

here is a more concise and readable solution using .dt.month_start() and .dt.month_end():

df = pl.DataFrame({'DateColumn': ['2022-02-13']})

test_df = df.with_columns([
    pl.col('DateColumn').str.strptime(pl.Date).cast(pl.Date)
])

test_df.with_columns(
   pl.col('DateColumn').dt.month_start().alias('MonthStart'),
   pl.col('DateColumn').dt.month_end().alias('MonthEnd')
)
shape: (1, 3)
┌────────────┬────────────┬────────────┐
 DateColumn  MonthStart  MonthEnd   
 ---         ---         ---        
 date        date        date       
╞════════════╪════════════╪════════════╡
 2022-02-13  2022-02-01  2022-02-28 
└────────────┴────────────┴────────────┘

huangapple
  • 本文由 发表于 2023年2月14日 08:05:27
  • 转载请务必保留本文链接:https://go.coder-hub.com/75442308.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定