Thanks to visit codestin.com
Credit goes to github.com

Skip to content

[Bug]: Replacement for epoch2num behaves differently (does not accept arrays) #22579

Closed
@laolux

Description

@laolux

Bug summary

matplotlib.dates.epoch2num is deprecated. The new method suggested in the deprecation warning behaves differently: It does not accept arrays.

Code for reproduction

import matplotlib
import matplotlib.dates
import numpy
datelist = numpy.zeros((2))
datelist[0] = 123456789
datelist[1] = 123456789.1
print(matplotlib.dates.epoch2num(datelist))
import datetime
print(matplotlib.dates.date2num(matplotlib.dates.datetime.datetime.utcfromtimestamp(datelist)))

Actual outcome

The new method crashes with
TypeError: only integer scalar arrays can be converted to a scalar index

Expected outcome

The new method returns the same result as epoch2num: An numpy array with the correct values from the conversion.

Additional information

Workaround by hand is to convert the input array entry for entry and write it to an output array. Works, but is potentially slower (depending on the internals of epoch2num) and can be quite some work to implement everywhere where epoch2num needs to be replaced.

Note: everything works fine as long as only scalars are involved.

Operating system

Fedora

Matplotlib Version

3.5.1

Matplotlib Backend

No response

Python version

Python 3.10.2

Jupyter version

No response

Installation

Linux package manager

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions