datetime-series

In [1]:
import pandas as pd
import numpy as np
In [2]:
df = pd.DataFrame({"date_range": pd.date_range("2016-05-01", "2016-05-02")})
df["date_range_str"] = df.date_range.copy().dt.strftime("%Y-%m-%d")
df["date_range_str_nan"] = df.date_range_str.copy()
df.loc[0, "date_range_str_nan"] = np.nan
df
Out[2]:
date_range date_range_str date_range_str_nan
0 2016-05-01 2016-05-01 NaN
1 2016-05-02 2016-05-02 2016-05-02
In [3]:
df.info()
<class 'pandas.core.frame.DataFrame'>
RangeIndex: 2 entries, 0 to 1
Data columns (total 3 columns):
date_range            2 non-null datetime64[ns]
date_range_str        2 non-null object
date_range_str_nan    1 non-null object
dtypes: datetime64[ns](1), object(2)
memory usage: 128.0+ bytes
In [4]:
[
    hasattr(df.date_range, "dt"),
    hasattr(df.date_range, "str"),
    hasattr(df.date_range_str, "dt"),
    hasattr(df.date_range_str, "str"),
]
Out[4]:
[True, False, False, True]
In [5]:
pd.to_datetime(df.date_range_str)
Out[5]:
0   2016-05-01
1   2016-05-02
Name: date_range_str, dtype: datetime64[ns]
In [6]:
pd.to_datetime(df.date_range_str_nan)
Out[6]:
0          NaT
1   2016-05-02
Name: date_range_str_nan, dtype: datetime64[ns]
In [7]:
df.date_range_str.astype("datetime64")
Out[7]:
0   2016-05-01
1   2016-05-02
Name: date_range_str, dtype: datetime64[ns]
In [8]:
try:
    df.date_range_str_nan.astype("datetime64")
except Exception as e:
    print(e)
Could not convert object to NumPy datetime
In [9]:
import tempfile
f = tempfile.NamedTemporaryFile()
df.to_csv(f.name, index=False)
df_temp = pd.read_csv(f.name, parse_dates=list(df.columns))
df_temp

Out[9]:
date_range date_range_str date_range_str_nan
0 2016-05-01 2016-05-01 NaT
1 2016-05-02 2016-05-02 2016-05-02
In [10]:
df_temp.info()
<class 'pandas.core.frame.DataFrame'>
RangeIndex: 2 entries, 0 to 1
Data columns (total 3 columns):
date_range            2 non-null datetime64[ns]
date_range_str        2 non-null datetime64[ns]
date_range_str_nan    1 non-null datetime64[ns]
dtypes: datetime64[ns](3)
memory usage: 128.0 bytes
In [11]:
# https://twitter.com/ksomemo/status/730296410868985856