I have a pandas DataFrame which has a column of dtype datetime:
import pandas as pd
# Mock-up data
df = pd.DataFrame({'year': [2015, 2016],
'month': [2, 3],
'day': [4, 5]})
df = pd.to_datetime(df)
print(df)
# 0 2015-02-04
# 1 2016-03-05
# dtype: datetime64[ns]
I would like to use the .to_markdown()
method to display this DataFrame.
However, the .to_markdown()
method displays the datetimes in scientific notation:
print(df.to_markdown())
# | | 0 |
# |---:|------------:|
# | 0 | 1.42301e+18 |
# | 1 | 1.45714e+18 |
Is there a way to have the .to_markdown()
method display these dates in a
more human-readable manner? The .to_latex()
, .to_csv()
, and
.to_string()
methods already behave this way:
# Other .to_ methods behave as desired, eg.
print(df.to_latex())
# \begin{tabular}{ll}
# \toprule
# {} & 0 \\
# \midrule
# 0 & 2015-02-04 \\
# 1 & 2016-03-05 \\
# \bottomrule
# \end{tabular}
pandas version: 1.3.2
tabulate version: 0.8.9
Under the hood the .to_markdown()
method uses the tabulate
package. The floatfmt
named argument can be used to control the formatting of floats, but I cannot see how this could be useful here.
The best solution I can currently find is simply to format the datetime column as a column of strings before calling the .to_markdown()
method:
print(df.astype(str).to_markdown())
# | | 0 |
# |---:|:-----------|
# | 0 | 2015-02-04 |
# | 1 | 2016-03-05 |