Line data Source code
1 : // SPDX-License-Identifier: BSD-3-Clause 2 : // Copyright (c) 2023 Scipp contributors (https://github.com/scipp) 3 : /// @file 4 : /// @author Jan-Lukas Wynen 5 : 6 : #include "numpy.h" 7 : 8 : #include "dtype.h" 9 : 10 662 : void ElementTypeMap<scipp::core::time_point>::check_assignable( 11 : const py::object &obj, const units::Unit unit) { 12 662 : const auto &dtype = obj.cast<py::array>().dtype(); 13 662 : if (dtype.attr("kind").cast<char>() == 'i') { 14 46 : return; // just assume we can assign from int 15 : } 16 : const auto np_unit = 17 616 : parse_datetime_dtype(dtype.attr("name").cast<std::string>()); 18 616 : if (np_unit != unit) { 19 42 : std::ostringstream oss; 20 0 : oss << "Unable to assign datetime with unit " << to_string(np_unit) 21 42 : << " to " << to_string(unit); 22 42 : throw std::invalid_argument(oss.str()); 23 42 : } 24 662 : } 25 : 26 9 : scipp::core::time_point make_time_point(const pybind11::buffer &buffer, 27 : const int64_t scale) { 28 : // buffer.cast does not always work because numpy.datetime64.__int__ 29 : // delegates to datetime.datetime if the unit is larger than ns and 30 : // that cannot be converted to long. 31 : using PyType = typename ElementTypeMap<core::time_point>::PyType; 32 18 : return core::time_point{ 33 9 : buffer.attr("astype")(py::dtype::of<PyType>()).cast<PyType>() * scale}; 34 : }