0

pd.Timestamp.floor() is not returning the floor of the timestamp, but is rounding up.

import datetime

timestamp = datetime.time(8, 0, 0, tzinfo=pytz.timezone("America/New_York"))
dt = datetime.datetime.combine(
        datetime.date(2021, 7, 3), timestamp
    ) + datetime.timedelta(hours=1, minutes=30)

# This gives: 2021-07-03 09:30:00-04:56
print(pd.Timestamp(dt))

# This gives: Timestamp('2021-07-03 10:00:00-0400', tz='America/New_York')
print(pd.Timestamp(dt).floor(freq='H')) 

When the dt represents 9:30am, why is the floor giving Timestamp('2021-07-03 10:00:00-0400', tz='America/New_York'), instead of Timestamp('2021-07-03 09:00:00-0400', tz='America/New_York')?

FObersteiner
  • 22,500
  • 8
  • 42
  • 72
1step1leap
  • 535
  • 1
  • 6
  • 11
  • Seems to have something to do with the timezone. Leaving it out produced the correct result. I've never used `pytz` before, so I'm not sure what the issue is. Would it be an option for you to attach timezone information after the floor operation? – ddejohn Oct 27 '21 at 05:18
  • you should neither use the datetime module nor pytz here. `pd.Timestamp("2021-07-03 09:30:00").tz_localize("America/New_York").floor('H')` works fine. – FObersteiner Oct 27 '21 at 05:34
  • The supposed duplicate is really not what this question is about. The OP hasn't even mentioned the strange initial time zone offset. The interesting thing is that it was initially formatted in standard time, and reformatted with daylight savings at the same time as rounded down, so looks like it is being rounded up. – Alex028502 Oct 27 '21 at 09:16

1 Answers1

0

When you generate dt it looks like you are getting standard time. This is because when you apply the time zone, it doesn't know the date yet.

In the second one you are converting to daylight by applying the timezone once it already knows the date

The former one is like 1430 GMT. The second is 1400 GMT.

I can't figure out why the offset is four minutes off though. I guess in 1970, new York time was only 4:56 behind what we call GMT today

I think if you change 2021-07-03 to 2021-12-03 you will get the answer you expect

Alex028502
  • 3,486
  • 2
  • 23
  • 50
  • `4:56` is the local mean time (LMT) offset, which is the first entry from the data base, see [P. Eggert's tz repo](https://github.com/eggert/tz/blob/8b409e22d7e1b70bf9364b014a8e359908a507a9/northamerica#L357). The point is that with pytz, you must *localize*, as the dupe question's answer describe. – FObersteiner Oct 27 '21 at 05:51