diff --git a/CHANGELOG.md b/CHANGELOG.md index eb435792c2b8fdde27203f52ff7abc287eda3dca..0d7f77a931cd917cc976b9af29f3e88e9dd2a53d 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -8,7 +8,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ### Added ### -* `send_mail` function in `caosdbadvanced.serverside.helper` module +* `send_mail` function in `caosadvancedtools.serverside.helper` module - New class to collect possible problems with the data model - New class for checking and importing tables - Function to get a file path to a shared resource directory @@ -19,6 +19,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ### Changed ### +* The `caosadvancedtools.table_importer.date_converter` now actually returns + `datetime.date` instance. A new + `caosadvancedtools.table_importer.datetime_converter` replaces the old + `date_converter` and returns a `datetime.datetime` instance. - The suppression module is now a logging filter. - The WebUIHandler is now a python logging formatter. - instead of `get_entity`, type-specific functions are used in diff --git a/src/caosadvancedtools/table_importer.py b/src/caosadvancedtools/table_importer.py index 03b4ffbc1549e008684e58158a8a57bd055237ad..bc1c2452c10db255f4dd4f4b1fb84b2ddeee5af5 100755 --- a/src/caosadvancedtools/table_importer.py +++ b/src/caosadvancedtools/table_importer.py @@ -71,7 +71,7 @@ def yes_no_converter(val): "Field should be 'Yes' or 'No', but is '{}'.".format(val)) -def date_converter(val, fmt="%Y-%m-%d"): +def datetime_converter(val, fmt="%Y-%m-%d %H:%M:%S"): """ if the value is already a datetime, it is returned otherwise it converts it using format string """ @@ -82,6 +82,14 @@ def date_converter(val, fmt="%Y-%m-%d"): return datetime.strptime(val, fmt) +def date_converter(val, fmt="%Y-%m-%d"): + """ if the value is already a datetime, it is returned otherwise it + converts it using format string + """ + + return datetime_converter(val, fmt=fmt).date() + + def win_path_list_converter(val): """ checks whether the value looks like a list of windows paths and converts diff --git a/unittests/date.xlsx b/unittests/date.xlsx index c852cde999b0014aece605328a407481e0de24b5..3402631e9cf48f0a08bd4e6269fcd9a01ae18472 100644 Binary files a/unittests/date.xlsx and b/unittests/date.xlsx differ diff --git a/unittests/test_table_importer.py b/unittests/test_table_importer.py index 3d44b0dc5fb9a229e1b79fe114082b2fc33a8c23..db40361c481365b8d88a7932e4f68048db22c5bb 100644 --- a/unittests/test_table_importer.py +++ b/unittests/test_table_importer.py @@ -21,12 +21,14 @@ import os import unittest from functools import partial from tempfile import NamedTemporaryFile +import datetime import numpy as np import pandas as pd from caosadvancedtools.datainconsistency import DataInconsistencyError from caosadvancedtools.table_importer import (XLSImporter, assure_name_format, date_converter, + datetime_converter, win_path_converter, win_path_list_converter, yes_no_converter) @@ -60,6 +62,17 @@ class ConverterTest(unittest.TestCase): r"\this\computer,\this\computer"), ["/this/computer", "/this/computer"]) + def test_datetime(self): + test_file = os.path.join(os.path.dirname(__file__), "date.xlsx") + self.importer = XLSImporter(converters={'d': datetime_converter, + }, obligatory_columns=['d']) + + xls_file = pd.io.excel.ExcelFile(test_file) + df = xls_file.parse() + df = self.importer.read_xls(test_file) + assert df.shape[0] == 2 + assert df.d.iloc[0] == datetime.datetime(1980,12,31,13,24,23) + def test_date(self): test_file = os.path.join(os.path.dirname(__file__), "date.xlsx") self.importer = XLSImporter(converters={'a': date_converter,