@@ -242,7 +242,8 @@ class Annotations:
242242 the annotations with raw data if their acquisition is started at the
243243 same time. If it is a string, it should conform to the ISO8601 format.
244244 More precisely to this '%%Y-%%m-%%d %%H:%%M:%%S.%%f' particular case of
245- the ISO8601 format where the delimiter between date and time is ' '.
245+ the ISO8601 format where the delimiter between date and time is ' ' and at most
246+ microsecond precision (nanoseconds are not supported).
246247 %(ch_names_annot)s
247248
248249 .. versionadded:: 0.23
@@ -390,6 +391,20 @@ def __init__(
390391 extras = None ,
391392 ):
392393 self ._orig_time = _handle_meas_date (orig_time )
394+ if isinstance (orig_time , str ) and self ._orig_time is None :
395+ try : # only warn if `orig_time` is not the default '1970-01-01 00:00:00'
396+ if _handle_meas_date (0 ) == datetime .strptime (
397+ orig_time , "%Y-%m-%d %H:%M:%S"
398+ ).replace (tzinfo = timezone .utc ):
399+ pass
400+ except ValueError : # error if incorrect datetime format AND not the default
401+ warn (
402+ "The format of the `orig_time` string is not recognised. It "
403+ "must conform to the ISO8601 format with at most microsecond "
404+ "precision and where the delimiter between date and time is "
405+ f"' '. Got: { orig_time } . Defaulting `orig_time` to None." ,
406+ RuntimeWarning ,
407+ )
393408 self .onset , self .duration , self .description , self .ch_names , self ._extras = (
394409 _check_o_d_s_c_e (onset , duration , description , ch_names , extras )
395410 )
@@ -615,7 +630,7 @@ def to_data_frame(self, time_format="datetime"):
615630 dt = _handle_meas_date (0 )
616631 time_format = _check_time_format (time_format , valid_time_formats , dt )
617632 dt = dt .replace (tzinfo = None )
618- times = _convert_times (self .onset , time_format , dt )
633+ times = _convert_times (self .onset , time_format , meas_date = dt , drop_nano = True )
619634 df = dict (onset = times , duration = self .duration , description = self .description )
620635 if self ._any_ch_names ():
621636 df .update (ch_names = self .ch_names )
@@ -1486,7 +1501,13 @@ def _read_annotations_csv(fname):
14861501 "onsets in seconds."
14871502 )
14881503 except ValueError :
1489- pass
1504+ # remove nanoseconds for ISO8601 (microsecond) compliance
1505+ timestamp = pd .Timestamp (orig_time )
1506+ timespec = "microseconds"
1507+ if timestamp == pd .Timestamp (_handle_meas_date (0 )).astimezone (None ):
1508+ timespec = "auto" # use default timespec for `orig_time=None`
1509+ orig_time = timestamp .isoformat (sep = " " , timespec = timespec )
1510+
14901511 onset_dt = pd .to_datetime (df ["onset" ])
14911512 onset = (onset_dt - onset_dt [0 ]).dt .total_seconds ()
14921513 duration = df ["duration" ].values .astype (float )
0 commit comments