如何"漂亮打印"python pandas DatetimeIndex

mas*_*chu 2 python format datetime pandas

我是大熊猫的新手,仍然对它能做什么感到惊讶,虽然有时也会做事情;-)

我设法编写了一个小脚本,用于报告时间序列中遇到的缺失值的数量,无论是在每个月还是在系列的每一年.下面是使用一些虚拟数据进行演示的代码.

如果我打印出返回的结果(print cntyprint cntm),一切都看起来不错,但我想根据我的数据的分辨率格式化指数的日期时间值,也就是我希望拥有2000 1000 10 15的,而不是2000-12-31 1000 10 15为年产量2000-01 744 10 15为月产量.有没有一种简单的方法在pandas中执行此操作,或者我必须经历一些循环并在打印之前将事物转换为"普通"python.注意:我事先并不知道我有多少数据列,所以每行都有固定格式字符串对我来说不起作用.

import numpy as np
import pandas as pd
import datetime as dt


def make_data():
    """Make up some bogus data where we know the number of missing values"""
    time = np.array([dt.datetime(2000,1,1)+dt.timedelta(hours=i)
                     for i in range(1000)])
    wd = np.arange(0.,1000.,1.)
    ws = wd*0.2
    wd[[2,3,4,8,9,22,25,33,99,324]] = -99.9   # 10 missing values
    ws[[2,3,4,10,11,12,565,644,645,646,647,648,666,667,669]]  =-99.9 # 15 missing values
    data = np.array(zip(time,wd,ws), dtype=[('time', dt.datetime),
                                            ('wd', 'f4'), ('ws', 'f4')])
    return data


def count_miss(data):
    time = data['time']
    dff = pd.DataFrame(data, index=time)
    # two options for setting missing values:
    # 1) replace everything less or equal -99
    for c in dff.columns:
        ser = pd.Series(dff[c])
        ser[ser <= -99.] = np.nan
        dff[c] = ser
    # 2) alternative: if you know the exact value to be replaced
    # you can use the DataFrame replace method:
##    dff.replace(-99.9, np.nan, inplace=True)

    # add the time variable as data column
    dff['time'] = time
    # count missing values
    # the print expressions will print date labels and the total number of values
    # in the time column plus the number of missing values for all other columns
    # annually:
    cnty = dff.resample('A', how='count', closed='right', label='right')
    for c in cnty.columns:
        if c != 'time':
            cnty[c] = cnty['time']-cnty[c]
    # monthly:
    cntm = dff.resample('M', how='count', closed='right', label='right')
    for c in cntm.columns:
        if c != 'time':
            cntm[c] = cntm['time']-cntm[c]
    return cnty, cntm

if __name__ == "__main__":
    data = make_data()
    cnty, cntm = count_miss(data)
Run Code Online (Sandbox Code Playgroud)

最后注意:是有一种DatetimeIndex的格式方法,但遗憾的是没有解释如何使用它.

Pau*_*l H 5

所述format的方法DatetimeIndex同样地进行strftime一个的datetime.datetime对象.

这意味着你可以使用这里找到的格式字符串:http://www.tutorialspoint.com/python/time_strftime.htm

诀窍是你必须传递方法的函数formatterkwarg format.看起来像这样(仅作为与您的代码无关的示例:

import pandas
dt = pandas.DatetimeIndex(periods=10, start='2014-02-01', freq='10T')
dt.format(formatter=lambda x: x.strftime('%Y    %m    %d  %H:%M.%S'))
Run Code Online (Sandbox Code Playgroud)

输出:

['2014    02    01  00:00.00',
 '2014    02    01  00:10.00',
 '2014    02    01  00:20.00',
 '2014    02    01  00:30.00',
 '2014    02    01  00:40.00',
 '2014    02    01  00:50.00',
 '2014    02    01  01:00.00',
 '2014    02    01  01:10.00',
 '2014    02    01  01:20.00',
 '2014    02    01  01:30.00']
Run Code Online (Sandbox Code Playgroud)