Skip to content

Commit

Permalink
Support datetime64 and timedelta64
Browse files Browse the repository at this point in the history
  • Loading branch information
fbriol committed Aug 3, 2021
1 parent 1927eca commit ce7c23b
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 0 deletions.
3 changes: 3 additions & 0 deletions fsspec/mapping.py
Original file line number Diff line number Diff line change
Expand Up @@ -178,6 +178,9 @@ def __reduce__(self):
def maybe_convert(value):
if isinstance(value, array.array) or hasattr(value, "__array__"):
# bytes-like things
if hasattr(value, "dtype") and value.dtype.kind in ["M", "m"]:
# numpy datetime64/timedelta64
value = value.view("i8")
value = bytearray(memoryview(value))
return value

Expand Down
24 changes: 24 additions & 0 deletions fsspec/tests/test_mapping.py
Original file line number Diff line number Diff line change
Expand Up @@ -119,3 +119,27 @@ def test_setitem_numpy():
assert m["c"] == b"\x01\x00\x00\x00"
m["c"] = np.array([1, 2], dtype="<i4") # array
assert m["c"] == b"\x01\x00\x00\x00\x02\x00\x00\x00"
m["c"] = np.array(
np.datetime64("2000-01-01T23:59:59.999999999"), dtype="<M8[ns]"
) # datetime64 scalar
assert m["c"] == b"\xff\xff\x91\xe3c\x9b#\r"
m["c"] = np.array(
[
np.datetime64("1900-01-01T23:59:59.999999999"),
np.datetime64("2000-01-01T23:59:59.999999999"),
],
dtype="<M8[ns]",
) # datetime64 array
assert m["c"] == b"\xff\xff}p\xf8fX\xe1\xff\xff\x91\xe3c\x9b#\r"
m["c"] = np.array(
np.timedelta64(3155673612345678901, "ns"), dtype="<m8[ns]"
) # timedelta64 scalar
assert m["c"] == b"5\x1c\xf0Rn4\xcb+"
m["c"] = np.array(
[
np.timedelta64(450810516049382700, "ns"),
np.timedelta64(3155673612345678901, "ns"),
],
dtype="<m8[ns]",
) # timedelta64 scalar
assert m["c"] == b',M"\x9e\xc6\x99A\x065\x1c\xf0Rn4\xcb+'

0 comments on commit ce7c23b

Please sign in to comment.