Description
Pandas version checks
-
I have checked that this issue has not already been reported.
-
I have confirmed this bug exists on the latest version of pandas.
-
I have confirmed this bug exists on the main branch of pandas.
Reproducible Example
Example with two columns for the dates and unexpected result.
from io import StringIO
import pandas as pd
data = '''day,time,value
2020-01-01,14:00:00+01:00,1
2020-01-01,14:15:00+01:00,1
2020-01-01,14:30:00+01:00,1
2020-01-01,14:45:00+01:00,1
2020-01-01,15:00:00+01:00,1
,,
'''
df = pd.read_csv(
StringIO(data),
sep=',',
parse_dates=[[0,1]],
index_col=0
)
print(df)
>>> value
day_time
2020-01-01 14:00:00+01:00 1.0
2020-01-01 14:15:00+01:00 1.0
2020-01-01 14:30:00+01:00 1.0
2020-01-01 14:45:00+01:00 1.0
2020-01-01 15:00:00+01:00 1.0
nan nan NaN
print(df.index)
>>> Index(['2020-01-01 14:00:00+01:00', '2020-01-01 14:15:00+01:00',
'2020-01-01 14:30:00+01:00', '2020-01-01 14:45:00+01:00',
'2020-01-01 15:00:00+01:00', 'nan nan'],
dtype='object', name='day_time')
Example with one column for the dates and expected result.
data = '''day_time,value
2020-01-01 14:00:00+01:00,1
2020-01-01 14:15:00+01:00,1
2020-01-01 14:30:00+01:00,1
2020-01-01 14:45:00+01:00,1
2020-01-01 15:00:00+01:00,1
,
'''
df = pd.read_csv(
StringIO(data),
sep=',',
parse_dates=[0],
index_col=0
)
print(df)
>>> value
day_time
2020-01-01 14:00:00+01:00 1.0
2020-01-01 14:15:00+01:00 1.0
2020-01-01 14:30:00+01:00 1.0
2020-01-01 14:45:00+01:00 1.0
2020-01-01 15:00:00+01:00 1.0
NaT NaN
print(df.index)
>>> DatetimeIndex(['2020-01-01 14:00:00+01:00', '2020-01-01 14:15:00+01:00',
'2020-01-01 14:30:00+01:00', '2020-01-01 14:45:00+01:00',
'2020-01-01 15:00:00+01:00', 'NaT'],
dtype='datetime64[ns, UTC+01:00]', name='day_time', freq=None)
Issue Description
I expect, that the returned DataFrame by pd.read_csv()
has a pd.DatetimeIndex
if I parse dates and there are some missing values in the date column, which should be NaT
.
Expected Behavior
What I see, is, that my expectations are fulfilled, if I parse only on column.
If I parse more than one column, the missing values are not returned as NaT
. At the end, the index of the index is of type Index
and not of type DatetimeIndex
.
Installed Versions
pandas : 2.1.3
numpy : 1.26.2
pytz : 2022.7.1
dateutil : 2.8.2
setuptools : 67.6.0
pip : 23.0.1
Cython : None
pytest : None
hypothesis : None
sphinx : None
blosc : None
feather : None
xlsxwriter : None
lxml.etree : 4.9.3
html5lib : None
pymysql : None
psycopg2 : None
jinja2 : 3.1.2
IPython : 8.11.0
pandas_datareader : None
bs4 : 4.11.2
bottleneck : None
dataframe-api-compat: None
fastparquet : None
fsspec : None
gcsfs : None
matplotlib : None
numba : None
numexpr : None
odfpy : None
openpyxl : None
pandas_gbq : None
pyarrow : None
pyreadstat : None
pyxlsb : None
s3fs : None
scipy : 1.11.4
sqlalchemy : 2.0.7
tables : None
tabulate : None
xarray : None
xlrd : None
zstandard : 0.19.0
tzdata : 2023.3
qtpy : None
pyqt5 : None