Skip to content

When transposing a datetime index without time and using to_html, time is added to column titles. #10640

@benlester

Description

@benlester

As outlined in my stackoverflow question, pandas to_html() doesn't behave consistently with a datetime index after being transposed.

A simple example:

import pandas as pd
import numpy as np

df = pd.DataFrame(np.random.randn(5, 5), columns = ['a', 'b', 'c', 'd', 'e'], index = ["2015-01-05","2015-01-04","2015-01-03","2015-01-02","2015-01-01"])
df.index = pd.to_datetime(df.index)

Then df.to_html() outputs:

<table border="1" class="dataframe">
  <thead>
    <tr style="text-align: right;">
      <th></th>
      <th>a</th>
      <th>b</th>
      <th>c</th>
      <th>d</th>
      <th>e</th>
    </tr>
  </thead>
  <tbody>
    <tr>
      <th>2015-01-05</th>
      <td>-0.025414</td>
      <td>0.001241</td>
      <td>-0.110182</td>
      <td>0.101301</td>
      <td>0.904187</td>
    </tr>
    <tr>
      <th>2015-01-04</th>
      <td>-0.113769</td>
      <td>1.490484</td>
      <td>0.386378</td>
      <td>0.152447</td>
      <td>0.440914</td>
    </tr>
    <tr>
      <th>2015-01-03</th>
      <td>-0.807287</td>
      <td>0.161482</td>
      <td>-0.255942</td>
      <td>0.041502</td>
      <td>-0.314685</td>
    </tr>
    <tr>
      <th>2015-01-02</th>
      <td>-0.849947</td>
      <td>0.600126</td>
      <td>-1.516112</td>
      <td>-0.571000</td>
      <td>0.013668</td>
    </tr>
    <tr>
      <th>2015-01-01</th>
      <td>-0.421461</td>
      <td>-0.287318</td>
      <td>-1.411263</td>
      <td>-0.088717</td>
      <td>-0.823639</td>
    </tr>
  </tbody>
</table>

Where as df.T.to_html() adds time (00:00:00) to the column titles:

<table border="1" class="dataframe">
  <thead>
    <tr style="text-align: right;">
      <th></th>
      <th>2015-01-05 00:00:00</th>
      <th>2015-01-04 00:00:00</th>
      <th>2015-01-03 00:00:00</th>
      <th>2015-01-02 00:00:00</th>
      <th>2015-01-01 00:00:00</th>
    </tr>
  </thead>
  <tbody>
    <tr>
      <th>a</th>
      <td>-0.025414</td>
      <td>-0.113769</td>
      <td>-0.807287</td>
      <td>-0.849947</td>
      <td>-0.421461</td>
    </tr>
    <tr>
      <th>b</th>
      <td>0.001241</td>
      <td>1.490484</td>
      <td>0.161482</td>
      <td>0.600126</td>
      <td>-0.287318</td>
    </tr>
    <tr>
      <th>c</th>
      <td>-0.110182</td>
      <td>0.386378</td>
      <td>-0.255942</td>
      <td>-1.516112</td>
      <td>-1.411263</td>
    </tr>
    <tr>
      <th>d</th>
      <td>0.101301</td>
      <td>0.152447</td>
      <td>0.041502</td>
      <td>-0.571000</td>
      <td>-0.088717</td>
    </tr>
    <tr>
      <th>e</th>
      <td>0.904187</td>
      <td>0.440914</td>
      <td>-0.314685</td>
      <td>0.013668</td>
      <td>-0.823639</td>
    </tr>
  </tbody>
</table>

This behaviour is inconsistent.

A remedy was found by EdChum on stackoverflow.

Metadata

Metadata

Assignees

No one assigned

    Labels

    BugIO HTMLread_html, to_html, Styler.apply, Styler.applymap

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions