如何将Zulu时区域字符串转换为特定的DateTime对象?

发布于 01-17 10:58 字数 783 浏览 5 评论 0原文

我有一个带有祖鲁时区的字符串格式的日期。我试图用正则表达式去掉“Z”字符,但我想有一种更有效的方法。

输入:

   |index | date                | municipality  
   |------| --------------------|--------------
   | 0    | 07.02.2021 1017Z    | Algier    
   | 1    | 11.01.2019 1716Z    | Abuja     
   | 2    | 23.02.2018 1002Z    | Brüssel   
   | 3    | 19.07.2021 1459Z    | Brüssel   
   | 4    | 26.11.2019 1049Z    | Berlin    

期望结果:

   |index | date                | municipality  
   |------| --------------------|--------------
   | 0    | 2021-02-17          | Algier    
   | 1    | 2019-01-11          | Abuja     
   | 2    | 2018-02-23          | Bruxelles     
   | 3    | 2021-07-19          | Bruxelles     
   | 4    | 2019-11-26          | Berlin    

I have a date in string format with zulu time zone. I tried to get rid of the "Z" character with regular expression, but I guess there is a more efficient way.

input:

   |index | date                | municipality  
   |------| --------------------|--------------
   | 0    | 07.02.2021 1017Z    | Algier    
   | 1    | 11.01.2019 1716Z    | Abuja     
   | 2    | 23.02.2018 1002Z    | Brüssel   
   | 3    | 19.07.2021 1459Z    | Brüssel   
   | 4    | 26.11.2019 1049Z    | Berlin    

desired outcome:

   |index | date                | municipality  
   |------| --------------------|--------------
   | 0    | 2021-02-17          | Algier    
   | 1    | 2019-01-11          | Abuja     
   | 2    | 2018-02-23          | Bruxelles     
   | 3    | 2021-07-19          | Bruxelles     
   | 4    | 2019-11-26          | Berlin    

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(3

哽咽笑 2025-01-24 10:58:33

不要删除 Z 字符,而是正确解析它。 EX:

import pandas as pd
df = pd.DataFrame({'date': ['07.02.2021 1017Z', '11.01.2019 1716Z']})
df['date'] = pd.to_datetime(df['date'], format='%d.%m.%Y %H%M%z')

# df['date']
# Out[19]: 
# 0   2021-02-07 10:17:00+00:00
# 1   2019-01-11 17:16:00+00:00
# Name: date, dtype: datetime64[ns, UTC]

请注意,设置 format 关键字是可选的,但显式指定它有助于提高总体可靠性。

如果您不想要的话,您也可以减少时间:

df['date'] = df['date'].dt.floor('D')

# df['date']
# Out[21]: 
# 0   2021-02-07 00:00:00+00:00
# 1   2019-01-11 00:00:00+00:00
# Name: date, dtype: datetime64[ns, UTC]

...或格式化为字符串:

df['date'].dt.strftime('%Y-%m-%d')
# 0    2021-02-07
# 1    2019-01-11
# Name: date, dtype: object

Instead of getting rid of the Z character, parse it correctly. EX:

import pandas as pd
df = pd.DataFrame({'date': ['07.02.2021 1017Z', '11.01.2019 1716Z']})
df['date'] = pd.to_datetime(df['date'], format='%d.%m.%Y %H%M%z')

# df['date']
# Out[19]: 
# 0   2021-02-07 10:17:00+00:00
# 1   2019-01-11 17:16:00+00:00
# Name: date, dtype: datetime64[ns, UTC]

Note that setting the format keyword is optional, but it helps for general reliability to specify it explicitly.

You can also floor the hours if you don't want them:

df['date'] = df['date'].dt.floor('D')

# df['date']
# Out[21]: 
# 0   2021-02-07 00:00:00+00:00
# 1   2019-01-11 00:00:00+00:00
# Name: date, dtype: datetime64[ns, UTC]

...or format to string:

df['date'].dt.strftime('%Y-%m-%d')
# 0    2021-02-07
# 1    2019-01-11
# Name: date, dtype: object
毁虫ゝ 2025-01-24 10:58:33

我想这会很好用。此外,您可以在转换期间对日期进行一些计算。

from datetime import datetime as dt


# specify input and output formats
input_format = '%d.%m.%Y %H%MZ'
output_format = '%Y-%m-%d'

# input date
input_date = '07.02.2021 1017Z'

# convert input date to datetime object
date = dt.strptime(input_date, input_format)

# convert datetime object to string with output format
output_date = dt.strftime(date, output_format)
print(output_date)

# output: 2021-02-07

This will works fine, I think. Additionally you can make some calculations with dates during transformation.

from datetime import datetime as dt


# specify input and output formats
input_format = '%d.%m.%Y %H%MZ'
output_format = '%Y-%m-%d'

# input date
input_date = '07.02.2021 1017Z'

# convert input date to datetime object
date = dt.strptime(input_date, input_format)

# convert datetime object to string with output format
output_date = dt.strftime(date, output_format)
print(output_date)

# output: 2021-02-07
仙气飘飘 2025-01-24 10:58:33

Alexei的方法也是一个很棒的解决方案,我们可以将其代码切换为函数并使用IT示例:

from datetime import datetime as dt

df=pd.DataFrame()
dates=['07.02.2021 1017Z','11.01.2019 1716Z','23.02.2018 1002Z']
municipality=['Algier','Abuja','Brüssel' ]

df['date']=dates
df['municipality']=municipality

# specify input and output formats
input_format = '%d.%m.%Y %H%M%z'
output_format = '%Y-%m-%d'

# input date
input_date = '07.02.2021 1017Z'

def convert(input_date):
  # convert input date to datetime object
  date = dt.strptime(input_date, input_format)

  # convert datetime object to string with output format
  output_date = dt.strftime(date, output_format)
  return(output_date)

df.date.apply(convert)
df

“在此处输入图像说明”

Alexei's method is a great solution also, we can switch its code into a function and use it example :

from datetime import datetime as dt

df=pd.DataFrame()
dates=['07.02.2021 1017Z','11.01.2019 1716Z','23.02.2018 1002Z']
municipality=['Algier','Abuja','Brüssel' ]

df['date']=dates
df['municipality']=municipality

# specify input and output formats
input_format = '%d.%m.%Y %H%M%z'
output_format = '%Y-%m-%d'

# input date
input_date = '07.02.2021 1017Z'

def convert(input_date):
  # convert input date to datetime object
  date = dt.strptime(input_date, input_format)

  # convert datetime object to string with output format
  output_date = dt.strftime(date, output_format)
  return(output_date)

df.date.apply(convert)
df

enter image description here

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文