C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML
Pandas DatetimeThe Pandas can provide the features to work with time-series data for all domains. It also consolidates a large number of features from other Python libraries like scikits.timeseries by using the NumPy datetime64 and timedelta64 dtypes. It provides new functionalities for manipulating the time series data. The time series tools are most useful for data science applications and deals with other packages used in Python. Example1:import pandas as pd # Create the dates with frequency info = pd.date_range('5/4/2013', periods = 8, freq ='S') info Output: DatetimeIndex(['2013-05-04 00:00:00', '2013-05-04 00:00:01', '2013-05-04 00:00:02', '2013-05-04 00:00:03', '2013-05-04 00:00:04', '2013-05-04 00:00:05', '2013-05-04 00:00:06', '2013-05-04 00:00:07'], dtype='datetime64[ns]', freq='S') Example2:info = pd.DataFrame({'year': [2014, 2012], 'month': [5, 7], 'day': [20, 17]}) pd.to_datetime(info) 0 2014-05-20 1 2012-07-17 dtype: datetime64[ns] You can pass errors='ignore' if the date does not meet the timestamp. It will return the original input without raising any exception. If you pass errors='coerce', it will force an out-of-bounds date to NaT. import pandas as pd pd.to_datetime('18000706', format='%Y%m%d', errors='ignore') datetime.datetime(1800, 7, 6, 0, 0) pd.to_datetime('18000706', format='%Y%m%d', errors='coerce') Output: Timestamp('1800-07-06 00:00:00') Example3:import pandas as pd dmy = pd.date_range('2017-06-04', periods=5, freq='S') dmy Output: DatetimeIndex(['2017-06-04 00:00:00', '2017-06-04 00:00:01', '2017-06-04 00:00:02', '2017-06-04 00:00:03', '2017-06-04 00:00:04'], dtype='datetime64[ns]', freq='S') Example4:import pandas as pd dmy = dmy.tz_localize('UTC') dmy Output: DatetimeIndex(['2017-06-04 00:00:00+00:00', '2017-06-04 00:00:01+00:00', '2017-06-04 00:00:02+00:00', '2017-06-04 00:00:03+00:00', '2017-06-04 00:00:04+00:00'], dtype='datetime64[ns, UTC]', freq='S') Example5:import pandas as pd dmy = pd.date_range('2017-06-04', periods=5, freq='S') dmy Output: DatetimeIndex(['2017-06-04 00:00:00', '2017-06-04 00:00:01', '2017-06-04 00:00:02', '2017-06-04 00:00:03', '2017-06-04 00:00:04'], dtype='datetime64[ns]', freq='S')
Next TopicPandas Time Offset
|