Python Pandas - Create A Datetime With DateTimeIndex
Maybe your like
- Home
- Whiteboard
- Online Compilers
- Practice
- Articles
- AI Assistant
- Jobs
- Tools
- Corporate Training
- Courses
- Certifications
- Switch theme
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
To create a datetime, we will use the date_range(). The periods and the time zone will also be set with the frequency. At first, import the required libraries −
import pandas as pdDatetimeIndex with period 8 and frequency as M i.e. months. The timezone is Australia/Sydney −
datetime = pd.date_range('2021-09-24 02:35:55', periods=8, tz='Australia/Sydney', freq='M')Display the datetime −
print("DateTime...\n", datetime)Example
Following is the code −
import pandas as pd # DatetimeIndex with period 8 and frequency as M i.e. months # timezone is Australia/Sydney datetime = pd.date_range('2021-09-24 02:35:55', periods=8, tz='Australia/Sydney', freq='M') # display print("DateTime...\n", datetime) # get the day name print("\nGetting the day name..\n",datetime.day_name()) # get the month name print("\nGetting the month name..\n",datetime.month_name()) # get the year print("\nGetting the year name..\n",datetime.year) # get the hour print("\nGetting the hour..\n",datetime.hour) # get the minutes print("\nGetting the minutes..\n",datetime.minute) # get the seconds print("\nGetting the seconds..\n",datetime.second)Output
This will produce the following output −
DateTime... DatetimeIndex(['2021-09-30 02:35:55+10:00', '2021-10-31 02:35:55+11:00', '2021-11-30 02:35:55+11:00', '2021-12-31 02:35:55+11:00', '2022-01-31 02:35:55+11:00', '2022-02-28 02:35:55+11:00', '2022-03-31 02:35:55+11:00', '2022-04-30 02:35:55+10:00'], dtype='datetime64[ns, Australia/Sydney]', freq='M') Getting the day name.. Index(['Thursday', 'Sunday', 'Tuesday', 'Friday', 'Monday', 'Monday','Thursday', 'Saturday'], dtype='object') Getting the month name.. Index(['September', 'October', 'November', 'December', 'January', 'February','March', 'April'], dtype='object') Getting the year name.. Int64Index([2021, 2021, 2021, 2021, 2022, 2022, 2022, 2022], dtype='int64') Getting the hour.. Int64Index([2, 2, 2, 2, 2, 2, 2, 2], dtype='int64') Getting the minutes.. Int64Index([35, 35, 35, 35, 35, 35, 35, 35], dtype='int64') Getting the seconds.. Int64Index([55, 55, 55, 55, 55, 55, 55, 55], dtype='int64')
AmitDiwan Updated on: 2021-10-19T08:38:54+05:30 5K+ Views
Kickstart Your Career
Get certified by completing the course
Get StartedTag » Add Datetimeindex To Pandas Dataframe
-
How Do I Properly Set The Datetimeindex For A Pandas Datetime ...
-
Pandas._frame — Pandas 1.4.3 Documentation
-
Pandas.DatetimeIndex — Pandas 1.4.3 Documentation
-
Pandas Time Series Examples: DatetimeIndex, PeriodIndex And ...
-
Python | Pandas _frame() - GeeksforGeeks
-
Adding Missing Dates In Datetime Index In Pandas DataFrame
-
Extract Month And Year To A New Column In Pandas - Data Interview Qs
-
Convert Pandas DatetimeIndex To String - Spark By {Examples}
-
Indexing Time Series Data In Pandas
-
Pandas Create Datetimeindex Between Dates Code Example
-
Set DatetimeIndex For Pandas DataFrame In Python (3 Examples)
-
Working With Pandas Datetime - Kanoki
-
ndas.DatetimeIndex - Apache Spark
-
Working With Datetime Index In Pandas | By Sergi Lehkyi