在熊猫中转换datetimeindex对象

时间:2019-11-13 17:09:28

标签: python pandas

我正在努力使用熊猫提取堆叠在以下DatetimeIndex对象中的唯一日期,我们将感谢社区的帮助。

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')

用于产生上述输出的代码是:

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

我希望我的输出看起来像这样:

'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'

1 个答案:

答案 0 :(得分:0)

如果只想打印出这些值的字符串表示形式,则可以对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