如何从python中的字符串创建日期时间索引?

时间:2019-06-05 06:18:38

标签: python-3.x string pandas datetime

共有三个文件,名称分别为:RewriteEngine On # Handle Authorization Header RewriteCond %{HTTP:Authorization} . RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}] # Redirect Trailing Slashes If Not A Folder... RewriteCond %{REQUEST_FILENAME} !-d RewriteCond %{REQUEST_URI} (.+)/$ RewriteRule ^ %1 [L,R=301] # Handle Front Controller... RewriteCond %{REQUEST_FILENAME} !-d RewriteCond %{REQUEST_FILENAME} !-f RewriteRule ^ index.php [L] RewriteEngine On RewriteCond %{HTTPS} off RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301] file_2018-01-01_01_temp.tiffile_2018-01-01_02_temp.tif。我想在python中将它们的名称列出为file_2018-01-01_03_temp.tif。 以下代码创建了错误的列表。

['2018010101', '2018010102', '2018010103']

结果: import pandas as pd from glob import glob from os import path pattern = '*.tif' filenames = [path.basename(x) for x in glob(pattern)] pd.DatetimeIndex([pd.Timestamp(f[5:9]) for f in filenames])

1 个答案:

答案 0 :(得分:2)

我认为最简单的是在列表理解中使用替换索引:

a = [f[5:18].replace('_','').replace('-','') for f in filenames]
print (a)
['2018010101', '2018010102', '2018010103']

类似于Series.str.replace

a = pd.Index([f[5:18] for f in filenames]).str.replace('\-|_', '')
print (a)
Index(['2018010101', '2018010102', '2018010103'], dtype='object')

或将值转换为DatetimeIndex,然后使用DatetimeIndex.strftime

a = pd.to_datetime([f[5:18] for f in filenames], format='%Y-%m-%d_%H').strftime('%Y%m%d%H')
print (a)
Index(['2018010101', '2018010102', '2018010103'], dtype='object')

编辑:

  

dtype在对象中,但必须在dtype ='datetime64 [ns]

如果需要日期时间,则格式必须为默认格式,无法更改:

d = pd.to_datetime([f[5:18] for f in filenames], format='%Y-%m-%d_%H')
print (d)
DatetimeIndex(['2018-01-01 01:00:00', '2018-01-01 02:00:00',
               '2018-01-01 03:00:00'],
              dtype='datetime64[ns]', freq=None)