PyQvd icon indicating copy to clipboard operation
PyQvd copied to clipboard

Attempting to call to_pandas() on a QVD with a column of type TIME is erroring out

Open cosmocracy opened this issue 1 year ago • 4 comments

Describe the bug Attempting to call to_pandas() on a QVD with a column of type TIME is erroring out

To Reproduce Steps to reproduce the behavior:

    table = QvdTable.from_qvd(qvd_path)
    df = table.to_pandas()

Expected behavior A working dataframe

Error

<class 'ValueError'>, ValueError('hour must be in 0..23')
_serial_number_to_time, qvd.py:714
time, qvd.py:617
_get_value_from_symbol, qvd.py:2625
<listcomp>, qvd.py:2446
<listcomp>, qvd.py:2446
to_pandas, qvd.py:2446
<module>, upload_rn_staffing.py:144

My guess... For my test case, the serial_number value making its way into this method is 45916.75. The trailing 0.75 represents 18:00 (6PM) and matches up with the text stored in the QVD for that (dual) value.

From qvd.py:

    @staticmethod
    def _serial_number_to_time(serial_number: float) -> dt.time:
        # pylint: disable-next=invalid-name
        seconds = int(serial_number * 24 * 60 * 60)
        hours, seconds = divmod(seconds, 60 * 60)
        minutes, seconds = divmod(seconds, 60)

        return dt.time(hours, minutes, seconds)

Should be adjusted/fixed. Example/suggested fix:

import datetime as dt

def _serial_number_to_time(serial_number: float) -> dt.time:
    # Keep only the fractional (time-of-day) part; works for negatives too.
    frac = serial_number % 1.0

    # Convert fraction of a day to total microseconds (1 day = 86_400 seconds).
    total_us = round(frac * 86_400_000_000)  # microseconds in a day

    # If rounding hits exactly 24:00:00, wrap to 00:00:00.
    total_us %= 86_400_000_000

    hours, rem = divmod(total_us, 3_600_000_000)
    minutes, rem = divmod(rem,   60_000_000)
    seconds, micros = divmod(rem, 1_000_000)

    return dt.time(int(hours), int(minutes), int(seconds), int(micros))

cosmocracy avatar Sep 23 '25 21:09 cosmocracy

Since I needed to fix it in a hurry, I used the following monkey patch. I'd be happy to fork/fix and submit a PR.

import datetime as dt
from pyqvd.qvd import TimeValue
def _fixed_serial_number_to_time(serial_number: float) -> dt.time:
    frac = serial_number % 1.0                          # time-of-day only
    total_us = round(frac * 86_400_000_000)             # microseconds in a day
    total_us %= 86_400_000_000                          # wrap 24:00 → 00:00
    hours, rem = divmod(total_us, 3_600_000_000)
    minutes, rem = divmod(rem,   60_000_000)
    seconds, micros = divmod(rem, 1_000_000)
    return dt.time(int(hours), int(minutes), int(seconds), int(micros))

# Apply the patch
TimeValue._serial_number_to_time = staticmethod(_fixed_serial_number_to_time)

# (Optional) smoke tests
assert TimeValue._serial_number_to_time(0.75) == dt.time(18, 0, 0)
assert TimeValue._serial_number_to_time(45916.75) == dt.time(18, 0, 0)

cosmocracy avatar Sep 23 '25 21:09 cosmocracy

PS, thank you for using a static method! Good design

cosmocracy avatar Sep 23 '25 21:09 cosmocracy

Thanks for your detailed error analysis and solution proposal. That's obvious a bug. Feel free to submit a PR, if you don't have time, I'll fix the bug.

MuellerConstantin avatar Sep 24 '25 13:09 MuellerConstantin

This bug has been fixed with the latest patch version v2.2.2. Thanks to @cosmocracy for analyzing the bug and implementing the solution.

MuellerConstantin avatar Sep 28 '25 08:09 MuellerConstantin