English 中文(简体)
如何将几个月多的经抽取的地块转化为极地的频率?
原标题:How to convert extracted strings with multiple months into dates in Polars?

我的数据与以下数据相似。 我正试图将每一栏改为一个日期栏,但由于原封顶有两个月,我正在使用极地的<密码>,cast功能,以提取任何月或一年。 我知道如何打碎下文所示的具体月份,但在抽取职能之前和之后,每一件尝试(.strptime(pl.Date,“%B/%B %Y),.strptime(pl.Datetime,“%B/%B%Y)等)都没有工作。 如果你们有希望的话!

import polars as pl

df = pl.DataFrame(
    {
        "name": [
            "January/February 1994",
            "November/December 1996",
            "May/June 1999",
            "March/April 2005",
        ]
    }
)


out = df.with_columns(
    pl.col("name").str.extract(r"(w+)/").alias("start_month"),
    pl.col("name").str.extract(r"/(w+)").alias("end_month"),
    pl.col("name").str.extract(r"(d+)").alias("year"),
)


print(out)

shape: (4, 4)
┌────────────────────────┬─────────────┬───────────┬──────┐
│ name                   ┆ start_month ┆ end_month ┆ year │
│ ---                    ┆ ---         ┆ ---       ┆ ---  │
│ str                    ┆ str         ┆ str       ┆ str  │
╞════════════════════════╪═════════════╪═══════════╪══════╡
│ January/February 1994  ┆ January     ┆ February  ┆ 1994 │
│ November/December 1996 ┆ November    ┆ December  ┆ 1996 │
│ May/June 1999          ┆ May         ┆ June      ┆ 1999 │
│ March/April 2005       ┆ March       ┆ April     ┆ 2005 │
└────────────────────────┴─────────────┴───────────┴──────┘
最佳回答

它可能与在docs中发出警告/表示。

底部图书馆需要一个<代码>day>,以备:https://github.com/pola-rs/polars/issues/12427

可在数据中人工排出/插入<代码>1,并在表格中添加<代码>%d<>>。

df.with_columns(
    start = ("1 " + pl.col("name").str.replace("/.* ", "")).str.to_datetime("%d %B %Y"),
    end = pl.col("name").str.replace(".*/", "1 ").str.to_datetime("%d %B %Y")
)    
shape: (4, 3)
┌────────────────────────┬─────────────────────┬─────────────────────┐
│ name                   ┆ start               ┆ end                 │
│ ---                    ┆ ---                 ┆ ---                 │
│ str                    ┆ datetime[μs]        ┆ datetime[μs]        │
╞════════════════════════╪═════════════════════╪═════════════════════╡
│ January/February 1994  ┆ 1994-01-01 00:00:00 ┆ 1994-02-01 00:00:00 │
│ November/December 1996 ┆ 1996-11-01 00:00:00 ┆ 1996-12-01 00:00:00 │
│ May/June 1999          ┆ 1999-05-01 00:00:00 ┆ 1999-06-01 00:00:00 │
│ March/April 2005       ┆ 2005-03-01 00:00:00 ┆ 2005-04-01 00:00:00 │
└────────────────────────┴─────────────────────┴─────────────────────┘
问题回答

您不可能有<条码>pl.Date/pl.Datetime,从现在起,在座标上需要一个日期,下面几句可以帮助您。

out.with_columns(
    ((pl.col("year")  +  -  + pl.col("start_month") +  -01 ).str.to_date("%Y-%B-%d")).alias("start_date"),
    ((pl.col("year")  +  -  + pl.col("end_month") +  -01 ).str.to_date("%Y-%B-%d")).alias("end_month"),
)




相关问题
Mysql compaire two dates from datetime?

I was try to measure what is faster and what should be the best way to compare two dates, from datetime record in MySql db. There are several approaches how to grab a date from date time, but I was ...

iPhone Date Picker rolls over to 2010 on December 27th?

So I implemented a UIDatepicker in one of my applications for scheduling and autodialing teleconferences... everything is pretty much ready to go except, while testing I noticed that when the date ...

Convert date Python

I have MMDDYY dates, i.e. today is 111609 How do I convert this to 11/16/2009, in Python?

specifying date format when using $form->inputs() in CakePHP

I am wondering if there is a way to specify the date format in the forms created using CakePHP s $form->inputs(); Please note that this is not the individual $form->input() but instead $form->inputs() ...

NSDateFormat, super simple! Where am I screwing up?

Ok, this is really simple, maybe I m a getting a bit burnt out, but seems like it should work, Query XML feed, put out date string, format, display in a cell. The issue is I m a getting a NULL ...

sqlite writing a date into an email

I am exporting a date value from sqlite and placing it into an email. The date appears like this 279498721.322872 I am using Objective C in an Iphone App. Does anyone know how to make this export ...

热门标签