简体   繁体   English

在 pandas 中转换日期时间索引 object

[英]convert a datetimeindex object in pandas

I am struggling to extract the unique dates stacked in the following DatetimeIndex object using pandas, will appreciate help from the community.我正在努力使用 pandas 提取堆叠在以下 DatetimeIndex object 中的唯一日期,将感谢社区的帮助。

DatetimeIndex(['2019-03-01 10:17:37', '2019-03-02 10:17:37',
               '2019-03-03 10:17:37', '2019-03-04 10:17:37',
               '2019-03-05 10:17:37', '2019-03-06 10:17:37',
               '2019-03-07 10:17:37', '2019-03-08 10:17:37',
               '2019-03-09 10:17:37', '2019-03-10 10:17:37',
               ...
               '2019-11-02 10:17:37', '2019-11-03 10:17:37',
               '2019-11-04 10:17:37', '2019-11-05 10:17:37',
               '2019-11-06 10:17:37', '2019-11-07 10:17:37',
               '2019-11-08 10:17:37', '2019-11-09 10:17:37',
               '2019-11-10 10:17:37', '2019-11-11 10:17:37'],
              dtype='datetime64[ns]', length=256, freq='D')

Code used to produce the above output is:用于产生上述 output 的代码是:

def fillna_period(x):
    end =datetime.strptime(yesterday, '%Y-%m-%d')
    x['filled_dates'] = x.apply(lambda x: pd.date_range(x['activation_date'],end, freq='D'), axis=1)
    return x

I would want my output to look this way:我希望我的 output 看起来像这样:

'2019-03-01 10:17:37'
'2019-03-02 10:17:37'
'2019-03-03 10:17:37'
'2019-03-04 10:17:37'
'2019-03-05 10:17:37' 
'2019-03-06 10:17:37'
'2019-03-07 10:17:37' 
'2019-03-08 10:17:37'
'2019-03-09 10:17:37' 
'2019-03-10 10:17:37'

If you just want to print out the string representation of those values, you can just use a small list comprehension with print :如果您只想打印出这些值的字符串表示,您可以使用带有print的小列表理解:

In [93]: [str(t) for t in x]
Out[93]:
['2019-03-01 10:17:37',
 '2019-03-02 10:17:37',
 '2019-03-03 10:17:37',
 '2019-03-04 10:17:37',
 '2019-03-05 10:17:37',
 '2019-03-06 10:17:37',
 '2019-03-07 10:17:37',
 '2019-03-08 10:17:37',
 '2019-03-09 10:17:37',
 '2019-03-10 10:17:37']

In [94]: print('\n'.join([str(t) for t in x]))
2019-03-01 10:17:37
2019-03-02 10:17:37
2019-03-03 10:17:37
2019-03-04 10:17:37
2019-03-05 10:17:37
2019-03-06 10:17:37
2019-03-07 10:17:37
2019-03-08 10:17:37
2019-03-09 10:17:37
2019-03-10 10:17:37

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM