3

I am having pandas dataframe with datetime index. I am trying to normalize it to midnight. https://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.Series.dt.normalize.html What I get is the following

print('before')
print(df.head())
df.index=df.index.normalize()
print('after')
print(df.head())

before
                     cabin    hx
2000-01-01 00:20:51   21.1  19.1
2000-01-01 00:21:01   21.1  19.1
2000-01-01 00:21:11   21.1  19.2
2000-01-01 00:21:21   21.1  19.1
2000-01-01 00:21:31   21.1  17.9
after
            cabin    hx
2000-01-01   21.1  19.1
2000-01-01   21.1  19.1
2000-01-01   21.1  19.2
2000-01-01   21.1  19.1
2000-01-01   21.1  17.9

So hour, min and sec are dropped away. Help appreciated.

Lasse
  • 111
  • 1
  • 9
  • This is just a display issue, if you look at the values they will all show times of `00:00` check `print(df.index[0])` – EdChum Feb 08 '19 at 16:35
  • Thanks. print(df.index[0]) prints 2000-01-01 00:00:00. But when I pass the frame to plotly all indexes are without hour, min and sec. If I do this: for a in range(0,10): print(df.index[a]) I get this: 2000-01-01 00:00:00 2000-01-01 00:00:00 2000-01-01 00:00:00 2000-01-01 00:00:00 2000-01-01 00:00:00 2000-01-01 00:00:00 2000-01-01 00:00:00 2000-01-01 00:00:00 2000-01-01 00:00:00 2000-01-01 00:00:00 – Lasse Feb 08 '19 at 16:47
  • Is there a reason to display the time when they are all the same? It seems redundant, you can change the dateformat by specifying a format: https://stackoverflow.com/questions/43968985/changing-the-formatting-of-a-datetime-axis-in-matplotlib/43969357 – EdChum Feb 08 '19 at 16:49
  • I think it is not redundant. If you look at original index, it is datetime with 10 sec step. Hour, sec min values are not the same. – Lasse Feb 08 '19 at 16:56
  • Then I don't understand your question because normalize means to set the time to `00:00` so you need to now update your question and explain clearly what you really want or you misunderstand what `normalize` does – EdChum Feb 08 '19 at 16:59
  • I have misunderstood normalize method. I thought it would shift the values starting from midnight. What it does is what I have got. All index values are midnight. – Lasse Feb 08 '19 at 17:16

0 Answers0