Skip to content

Timestamp rounding wrong implementation #11963

Closed
@Tux1

Description

@Tux1

Hi,
I was looking at the recent ENH #4314 and there is a wrong implementation of round method for datetime.

    def round(self, freq):
            """
            return a new Timestamp rounded to this resolution
            Parameters
            ----------
            freq : a freq string indicating the rouding resolution
            """
            cdef int64_t unit
            cdef object result, value

            from pandas.tseries.frequencies import to_offset
            unit = to_offset(freq).nanos
            if self.tz is not None:
                value = self.tz_localize(None).value
            else:
                value = self.value
            result = Timestamp(unit*np.floor(value/unit),unit='ns')
            if self.tz is not None:
                result = result.tz_localize(self.tz)
            return result

You are flooring the nano timestamp instead of rounding. You should replace np.floor by np.round.
Also I propose to add floor and ceil method to Timestamp in order to have the same behavior that float. (It is very usefull to get the upper/lower bound for a specific freq, likely more usefull than round)

Do you agree @jreback ?

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions