datajoint-python
datajoint-python copied to clipboard
More detailed treatment of time datatypes
trafficstars
Feature Request
Problem
- Currently, the
TIMEMySQL type is used to store the datajointtimetype. - There is also currently no support for a
timedeltatype. - Datajoint queries of
timetypes returndatetime.timedeltatypes. - This is because the
TIMEMySQL type is made to support bothtimeandtimedeltatypes, and PyMySQL resolves this by defaulting totimedelta. - Since the DataJoint
timetype nominally only supportsHH:MM:SS"time of day", this should be reflected by casting this return to adatetime.timeobject. - Although
timetypes are returned astimedeltaobjects, attempting to insert atimedeltaobject results in anAttributeError, as a translation method fordatetime.timedelta->timehas not been implemented. - Given the underlying support for
timedeltatypes in the domain[-838:59:59, 838:59:59]and the meaningfulness of timedelta as a datatype, atimedeltatype should be implemented which is also stored as aTIMEtype in the underlying database.
Requirements
There are a couple viable solutions. Here are the cases I envision:
- If
timedeltais implemented, and I think it should, queries oftimetypes should be returned asdatetime.time. - If
timedeltais not implemented,timeshould be expanded to support timedelta types. To these ends, a translation method fordatetime.timedelta->timeshould be implemented. - If
timedeltais not implemented andtimeis not expanded to support timedelta types, then there is no reason that queries oftimeshould not be altered to returndatetime.timeobjects.
Justification
The concept of duration is ubiquitous in science. Given that this type is supported as both native python datetime.timedelta and the underlying MySQL type TIME, this should certainly be supported by DataJoint.
Furthermore, objects returned by fetch operations should constitute valid objects for the insert operation. For at least the time type, this is not the case.
Reproduction
In datajoint_python version 13.2:
- Create a schema with a table containing a
timeattribute such as
# test_schema.py
@schema
class Test(dj.Manual):
definition = """
test_id: int
---
test_time: time
"""
- After importing the schema, call insert on the table:
from test_schema import Test
import datetime
Test.insert1({'test_id': 1, 'test_time': datetime.datetime.now().time())}) # Should be fine
test_query = Test & 'test_id=1' # Query for inserted item
test_item = test_query.fetch1() # Fetch item
assert isinstance(test_item['test_time'], datetime.timedelta) # Assertion succeeds
test_query.delete()
Test.insert1(test_item) # raises AttributeError