Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to create a pandas Timestamp object?

It might sound like a trivial question, but I was not able to find proper documentation on this including help(pd.Timestamp) which does not provide a clear constructor.

I want to create the last time stamp of eg day 2012-12-13, so hour is 23, minute is 59, seconds is 59, nanosecond is 999999999

alternatively, if I have microsecond precision the last timestamp of a day would be as before except microsecond is 999999

I need this to filter out all timestamps until a given (end of) day by indexing the original series by something like df.ix[:last_timestamp]

Thx

like image 306
Mannaggia Avatar asked Feb 06 '14 10:02

Mannaggia


1 Answers

In [1]: Timestamp('20131213 11:59:59.999999999')
Out[1]: Timestamp('2013-12-13 11:59:59.999999', tz=None)

You can also do

In [3]: pd.Timestamp('20141213')-pd.Timedelta('1ns')
Out[3]: Timestamp('2014-12-12 23:59:59.999999999')

Sounds like you actually want to use partial string slicing, see here

In [19]: s = Series(1,pd.date_range('20131212',freq='H',periods=25))

In [20]: s
Out[20]: 
2013-12-12 00:00:00    1
2013-12-12 01:00:00    1
2013-12-12 02:00:00    1
                      ..
2013-12-12 22:00:00    1
2013-12-12 23:00:00    1
2013-12-13 00:00:00    1
Freq: H, dtype: int64

In [21]: s['2013':'20131212']
Out[21]: 
2013-12-12 00:00:00    1
2013-12-12 01:00:00    1
2013-12-12 02:00:00    1
                      ..
2013-12-12 21:00:00    1
2013-12-12 22:00:00    1
2013-12-12 23:00:00    1
Freq: H, dtype: int64
like image 159
Jeff Avatar answered Sep 28 '22 12:09

Jeff