Skip to content

Pandas 0.15.1 pandas.t0_datetime() sometimes returns pandas.core.series.Series and sometimes pandas.tseries.index.DatetimeIndex #8919

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

Closed
sorenwacker opened this issue Nov 28, 2014 · 3 comments
Labels
Datetime Datetime data dtype Usage Question

Comments

@sorenwacker
Copy link

pandas.to_datetime() should not return different types.
Otherwise, all output should have methods like .head() and .tail() and .month()


import pandas as pd
import numpy as np
pd.options.display.mpl_style = 'default'
import matplotlib.pyplot as plt
x0['Month']=x0['Date'].month
data=np.random.rand(5)
data[4]=NaN
dfdata=pd.DataFrame({'Data':data})
times=['19990103','19990106','19990109','19990112','NaT']
dftimes = pd.to_datetime(times,format='%Y%m%d')

dftimes

Out[10]:
class 'pandas.tseries.index.DatetimeIndex'

dftimes.head

AttributeError Traceback (most recent call last)
in ()
----> 1 dftimes.head
AttributeError: 'DatetimeIndex' object has no attribute 'head'


'Download files'
import urllib
urllib.urlretrieve ("http://www.epa.gov/ttn/airs/airsaqs/detaildata/501files/Rd_501_88101_1999.Zip", "./data/Rd_501_88101_1999.zip")
'Unzip the files...'
import zipfile
import os.path
zfile = zipfile.ZipFile("./data/Rd_501_88101_1999.zip")
for name in zfile.namelist():
zfile.extract(name,'./data/')

import pandas as pd
import numpy as np
pd.options.display.mpl_style = 'default'
import matplotlib.pyplot as plt
%pylab inline

pm0=pd.io.parsers.read_table("./data/RD_501_88101_1999-0.txt","|",index_col=False,low_memory=False).drop(0)
x0=pm0[['Sample Value']].astype(float)
t0=pd.to_datetime(pm0['Date'],format='%Y%m%d')
print type(t0)

class 'pandas.core.series.Series'

t0.head()

1 1999-01-03
2 1999-01-06
3 1999-01-09
4 1999-01-12
5 1999-01-15
Name: Date, dtype: datetime64[ns]

@jreback
Copy link
Contributor

jreback commented Nov 28, 2014

No sometimes about it; Its a function of the type of the input.

With a list-like, you will get a DatetimeIndex
with a scalar, you will get a Timestamp
with a Series you will get a Series

So you get an output of like-kind to the input.

In [13]: pd.to_datetime(['20140101'])
Out[13]: 
<class 'pandas.tseries.index.DatetimeIndex'>
[2014-01-01]
Length: 1, Freq: None, Timezone: None

In [14]: pd.to_datetime(Series(['20140101']))
Out[14]: 
0   2014-01-01
dtype: datetime64[ns]

@jreback jreback closed this as completed Nov 28, 2014
@jreback jreback added Usage Question Datetime Datetime data dtype labels Nov 28, 2014
@jorisvandenbossche
Copy link
Member

Maybe this should be added to the docstring?

@jorisvandenbossche
Copy link
Member

@jreback @teramind see #8921

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

No branches or pull requests

3 participants