0

there is an incorrect date - 44288.

In excel i can change format and get 02.04.2021, but how to get this result in pandas?

anon.for
  • 57
  • 5
  • Does this answer your question? [Convert Excel style date with pandas](https://stackoverflow.com/questions/38454403/convert-excel-style-date-with-pandas) – BigBen Nov 09 '22 at 13:49

1 Answers1

1

This has been done similarly in: Convert Excel style date with pandas

However, with this you just add abs() to turn the negative integer into a positive, if you want it done for all in a column:

import datetime
import pandas as pd

df = pd.DataFrame({'date':[-44288,-44289]})

df['date'] = pd.TimedeltaIndex(abs(df['date']),unit='d') + datetime.datetime(1899, 12, 30)

Output:

        date
0 2021-04-02
1 2021-04-03
thmasXX
  • 103
  • 6
  • then just add ```df['date'] = df['date'].dt.strftime('%d.%m.%Y')``` if you'd like the correct format. – thmasXX Nov 09 '22 at 14:27
  • I suppose that the `-` is not actually in OP's data. Maybe a `:` would have been a better choice. – BigBen Nov 09 '22 at 14:29
  • @BigBen if there is a : before hand, if you know the indexes, could simply do ```df['date'][0] = int(df['date'][0].replace(':',''))```? – thmasXX Nov 09 '22 at 14:50
  • I meant a `:` in the question body, not in the actual data. My guess is that OP's *data* does not inlclude a `-`, as a negative date in Excel is meaningless. – BigBen Nov 09 '22 at 14:55
  • My bad @BigBen, sorry for the confusion. – thmasXX Nov 09 '22 at 15:33