Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cannot write LASFile which contains datetime64[ns] objects #492

Open
kinverarity1 opened this issue Oct 5, 2021 · 1 comment
Open

Cannot write LASFile which contains datetime64[ns] objects #492

kinverarity1 opened this issue Oct 5, 2021 · 1 comment
Labels

Comments

@kinverarity1
Copy link
Owner

Describe the bug
A LASFile object containing a curve of dtype datetime64[ns] cannot be written to a LAS file unless converted to strings.

To Reproduce
See failure for case #2 below:

from datetime import datetime
import lasio
import numpy as np
import traceback
import sys

dt_data_original = np.array([
    datetime(2021, 9, 29, 12, 0, 0), 
    datetime(2021, 9, 29, 12, 0, 1), 
    datetime(2021, 9, 29, 12, 0, 2)
], dtype=datetime)
dt_data_1 = dt_data_original
dt_data_2 = pd.Series(dt_data_original)
dt_data_3 = dt_data_2.astype(str)

for i, dt_data in enumerate([dt_data_1, dt_data_2, dt_data_3]):
    las = lasio.LASFile()
    las.add_curve("depth", data=[1, 2, 3])
    las.add_curve("datetimes", data=dt_data)
    las.add_curve("data", data=[10, 7, 12])

    print(f"=============\nCase #{i + 1}\n=============")
    print(type(dt_data))
    print(dt_data.dtype)
    print("\n")

    try:
        las.write(sys.stdout)
    except TypeError:
        print(traceback.format_exc())
=============
Case #1
=============
<class 'numpy.ndarray'>
object


~Version ---------------------------------------------------
VERS.   2.0 : CWLS log ASCII Standard -VERSION 2.0
WRAP.    NO : One line per depth step
DLM . SPACE : Column Data Section Delimiter
~Well ------------------------------------------------------
STRT.m 1.00000 : START DEPTH
STOP.m 3.00000 : STOP DEPTH
STEP.m 1.00000 : STEP
NULL. -9999.25 : NULL VALUE
COMP.          : COMPANY
WELL.          : WELL
FLD .          : FIELD
LOC .          : LOCATION
PROV.          : PROVINCE
CNTY.          : COUNTY
STAT.          : STATE
CTRY.          : COUNTRY
SRVC.          : SERVICE COMPANY
DATE.          : DATE
UWI .          : UNIQUE WELL ID
API .          : API NUMBER
~Curve Information -----------------------------------------
depth    .m  : 
datetimes.   : 
data     .   : 
~Params ----------------------------------------------------
~Other -----------------------------------------------------
~ASCII -----------------------------------------------------
    1.00000 2021-09-29 12:00:00   10.00000
    2.00000 2021-09-29 12:00:01    7.00000
    3.00000 2021-09-29 12:00:02   12.00000
=============
Case #2
=============
<class 'pandas.core.series.Series'>
datetime64[ns]


~Version ---------------------------------------------------
VERS.   2.0 : CWLS log ASCII Standard -VERSION 2.0
WRAP.    NO : One line per depth step
DLM . SPACE : Column Data Section Delimiter
~Well ------------------------------------------------------
STRT.m 1.00000 : START DEPTH
STOP.m 3.00000 : STOP DEPTH
STEP.m 1.00000 : STEP
NULL. -9999.25 : NULL VALUE
COMP.          : COMPANY
WELL.          : WELL
FLD .          : FIELD
LOC .          : LOCATION
PROV.          : PROVINCE
CNTY.          : COUNTY
STAT.          : STATE
CTRY.          : COUNTRY
SRVC.          : SERVICE COMPANY
DATE.          : DATE
UWI .          : UNIQUE WELL ID
API .          : API NUMBER
~Curve Information -----------------------------------------
depth    .m  : 
datetimes.   : 
data     .   : 
~Params ----------------------------------------------------
~Other -----------------------------------------------------
Traceback (most recent call last):
  File "<ipython-input-90-52992b419c0b>", line 28, in <module>
    las.write(sys.stdout)
  File "c:\devapps\kinverarity\projects\lasio\lasio\las.py", line 523, in write
    writer.write(self, file_ref, **kwargs)
  File "c:\devapps\kinverarity\projects\lasio\lasio\writer.py", line 213, in write
    data_arr = las.data
  File "c:\devapps\kinverarity\projects\lasio\lasio\las.py", line 823, in data
    return np.vstack([c.data for c in self.curves]).T
  File "<__array_function__ internals>", line 5, in vstack
  File "C:\ProgramData\miniconda3\envs\wrap\lib\site-packages\numpy\core\shape_base.py", line 283, in vstack
    return _nx.concatenate(arrs, 0)
  File "<__array_function__ internals>", line 5, in concatenate
TypeError: The DTypes <class 'numpy.dtype[datetime64]'> and <class 'numpy.dtype[int32]'> do not have a common DType. For example they cannot be stored in a single array unless the dtype is `object`.

=============
Case #3
=============
<class 'pandas.core.series.Series'>
object


~Version ---------------------------------------------------
VERS.   2.0 : CWLS log ASCII Standard -VERSION 2.0
WRAP.    NO : One line per depth step
DLM . SPACE : Column Data Section Delimiter
~Well ------------------------------------------------------
STRT.m 1.00000 : START DEPTH
STOP.m 3.00000 : STOP DEPTH
STEP.m 1.00000 : STEP
NULL. -9999.25 : NULL VALUE
COMP.          : COMPANY
WELL.          : WELL
FLD .          : FIELD
LOC .          : LOCATION
PROV.          : PROVINCE
CNTY.          : COUNTY
STAT.          : STATE
CTRY.          : COUNTRY
SRVC.          : SERVICE COMPANY
DATE.          : DATE
UWI .          : UNIQUE WELL ID
API .          : API NUMBER
~Curve Information -----------------------------------------
depth    .m  : 
datetimes.   : 
data     .   : 
~Params ----------------------------------------------------
~Other -----------------------------------------------------
~ASCII -----------------------------------------------------
    1.00000 2021-09-29 12:00:00   10.00000
    2.00000 2021-09-29 12:00:01    7.00000
    3.00000 2021-09-29 12:00:02   12.00000

Expected behavior
There shouldn't be an error. Obviously the writer code needs significant re-factoring, but in the meantime this is an unpleasant bug.

Software versions (please complete the following information):

  • Python version: [e.g. 3.6 32 bit]
  • Python distribution [e.g. Anaconda]
  • lasio version (import lasio; print(lasio.__version__)) : latest (0.30.dev74+g85bf606)

Additional context
Add any other context about the problem here.

@kinverarity1
Copy link
Owner Author

Of course.... even cases 1 and 2 are problematic because they will read back in incorrectly given they contain whitespace 😞

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

1 participant