如何在转置日期时间索引后停止Pandas为列标题添加时间?

时间:2015-07-20 15:09:20

标签: python pandas

我有一个Pandas数据框如下:

In [10]: libor_table
Out[10]:
           Euribor interest rate - 3 months Euribor interest rate - 6 months  \
2015-07-17                          -0.019%                           0.049%
2015-07-16                          -0.019%                           0.049%
2015-07-15                          -0.019%                           0.049%
2015-07-14                          -0.019%                           0.049%
2015-07-13                          -0.019%                           0.049%

           GBP LIBOR - 3 months GBP LIBOR - 6 months USD LIBOR - 3 months  \
2015-07-17             0.58375%             0.75406%             0.29175%
2015-07-16             0.58438%             0.75313%             0.28700%
2015-07-15             0.58406%             0.75063%             0.28850%
2015-07-14             0.58219%             0.74250%             0.28850%
2015-07-13             0.58188%             0.73750%             0.28880%

           USD LIBOR - 6 months
2015-07-17             0.46020%
2015-07-16             0.45570%
2015-07-15             0.46195%
2015-07-14             0.46345%
2015-07-13             0.46340%

索引是在datetime:

In [11]: libor_table.index
Out[11]:
DatetimeIndex(['2015-07-17', '2015-07-16', '2015-07-15', '2015-07-14',
               '2015-07-13'],
              dtype='datetime64[ns]', freq=None, tz=None)

我的问题是当我使用to_html()将表格变成HTML表格时。标准数据帧很好地转换为HTML表格:

<table border="1" class="dataframe">
  <thead>
    <tr style="text-align: right;">
      <th></th>
      <th>Euribor interest rate - 3 months</th>
      <th>Euribor interest rate - 6 months</th>
      <th>GBP LIBOR - 3 months</th>
      <th>GBP LIBOR - 6 months</th>
      <th>USD LIBOR - 3 months</th>
      <th>USD LIBOR - 6 months</th>
    </tr>
  </thead>
  <tbody>
    <tr>
      <th>2015-07-17</th>
      <td>-0.019%</td>
      <td>0.049%</td>
      <td>0.58375%</td>
      <td>0.75406%</td>
      <td>0.29175%</td>
      <td>0.46020%</td>
    </tr>
    <tr>
      <th>2015-07-16</th>
      <td>-0.019%</td>
      <td>0.049%</td>
      <td>0.58438%</td>
      <td>0.75313%</td>
      <td>0.28700%</td>
      <td>0.45570%</td>
    </tr>
    <tr>
      <th>2015-07-15</th>
      <td>-0.019%</td>
      <td>0.049%</td>
      <td>0.58406%</td>
      <td>0.75063%</td>
      <td>0.28850%</td>
      <td>0.46195%</td>
    </tr>
    <tr>
      <th>2015-07-14</th>
      <td>-0.019%</td>
      <td>0.049%</td>
      <td>0.58219%</td>
      <td>0.74250%</td>
      <td>0.28850%</td>
      <td>0.46345%</td>
    </tr>
    <tr>
      <th>2015-07-13</th>
      <td>-0.019%</td>
      <td>0.049%</td>
      <td>0.58188%</td>
      <td>0.73750%</td>
      <td>0.28880%</td>
      <td>0.46340%</td>
    </tr>
  </tbody>
</table>

但是,我想为HTML输出转换数据帧 - libor_table.transpose().to_html(),当我这样做时,pandas会将时间添加到列标题中,如下所示:

<table border="1" class="dataframe">
  <thead>
    <tr style="text-align: right;">
      <th></th>
      <th>2015-07-17 00:00:00</th>
      <th>2015-07-16 00:00:00</th>
      <th>2015-07-15 00:00:00</th>
      <th>2015-07-14 00:00:00</th>
      <th>2015-07-13 00:00:00</th>
    </tr>
  </thead>
  <tbody>
    <tr>
      <th>Euribor interest rate - 3 months</th>
      <td>-0.019%</td>
      <td>-0.019%</td>
      <td>-0.019%</td>
      <td>-0.019%</td>
      <td>-0.019%</td>
    </tr>
    <tr>
      <th>Euribor interest rate - 6 months</th>
      <td>0.049%</td>
      <td>0.049%</td>
      <td>0.049%</td>
      <td>0.049%</td>
      <td>0.049%</td>
    </tr>
    <tr>
      <th>GBP LIBOR - 3 months</th>
      <td>0.58375%</td>
      <td>0.58438%</td>
      <td>0.58406%</td>
      <td>0.58219%</td>
      <td>0.58188%</td>
    </tr>
    <tr>
      <th>GBP LIBOR - 6 months</th>
      <td>0.75406%</td>
      <td>0.75313%</td>
      <td>0.75063%</td>
      <td>0.74250%</td>
      <td>0.73750%</td>
    </tr>
    <tr>
      <th>USD LIBOR - 3 months</th>
      <td>0.29175%</td>
      <td>0.28700%</td>
      <td>0.28850%</td>
      <td>0.28850%</td>
      <td>0.28880%</td>
    </tr>
    <tr>
      <th>USD LIBOR - 6 months</th>
      <td>0.46020%</td>
      <td>0.45570%</td>
      <td>0.46195%</td>
      <td>0.46345%</td>
      <td>0.46340%</td>
    </tr>
  </tbody>
</table>

为什么熊猫这样做并且有办法阻止它吗?

编辑:此错误已提交here

1 个答案:

答案 0 :(得分:1)

这看起来像是一个我可以用一个小例子重现的错误:

In [120]:
# generate some dummy data
t="""time,value
2015-07-17,0
2015-07-18,1"""
df = pd.read_csv(io.StringIO(t), parse_dates=True, index_col=[0])
df

Out[120]:
            value
time             
2015-07-17      0
2015-07-18      1

在此处调用to_html按预期工作:

In [121]:   
df.to_html()

Out[121]:
'<table border="1" class="dataframe">\n  <thead>\n    <tr style="text-align: right;">\n      <th></th>\n      <th>value</th>\n    </tr>\n    <tr>\n      <th>time</th>\n      <th></th>\n    </tr>\n  </thead>\n  <tbody>\n    <tr>\n      <th>2015-07-17</th>\n      <td>0</td>\n    </tr>\n    <tr>\n      <th>2015-07-18</th>\n      <td>1</td>\n    </tr>\n  </tbody>\n</table>'

要解决转置格式问题,您可以明确将datetimeindex设置为date

In [122]:
df.index = df.index.date
df.T.to_html()

Out[122]:
'<table border="1" class="dataframe">\n  <thead>\n    <tr style="text-align: right;">\n      <th></th>\n      <th>2015-07-17</th>\n      <th>2015-07-18</th>\n    </tr>\n  </thead>\n  <tbody>\n    <tr>\n      <th>value</th>\n      <td>0</td>\n      <td>1</td>\n    </tr>\n  </tbody>\n</table>'