How to deal with Date & Time Basics in Python?
DATA MUNGING DATA CLEANING PYTHON MACHINE LEARNING RECIPES PANDAS CHEATSHEET     ALL TAGS

How to deal with Date & Time Basics in Python?

How to deal with Date & Time Basics in Python?

This recipe helps you deal with Date & Time Basics in Python

0

Recipe Objective

Have you tried to change data time or calculate some statistic from date time stamp.

So this is the recipe on how we can deal with Date & Time Basics in Python.

Step 1 - Import the library

from datetime import datetime from datetime import timedelta

We have imported datetime and timedelta which will be needed for the dataset.

Step 2 - Setting up the Data

We have used current datetime for this snippet. now = datetime.now() print(now)

Step 3 - Dealing with Date Time

Here we will be using different functions that we can use on date time.

  • Calculating Current Year
  • print(now.year)
  • Calculating current month
  • print(now.month)
  • Calculating current day
  • print(now.day)
  • Calculating current hour
  • print(now.hour)
  • Calculating current minute
  • print(now.minute)
  • Calculating difference between two days
  • delta = datetime(2011, 1, 7) - datetime(2011, 1, 6) print(delta.days)
  • Calculating difference in seconds
  • print(delta.seconds)
  • Creating a datetime
  • start = datetime(2018, 1, 7)
  • Adding twelve days to the time
  • print(start + timedelta(12))
So the output comes as:


2020-10-16 01:50:34.441904

2020

10

16

1

50

1 day, 0:00:00

1

0

2018-01-19 00:00:00

Relevant Projects

Choosing the right Time Series Forecasting Methods
There are different time series forecasting methods to forecast stock price, demand etc. In this machine learning project, you will learn to determine which forecasting method to be used when and how to apply with time series forecasting example.

Machine Learning project for Retail Price Optimization
In this machine learning pricing project, we implement a retail price optimization algorithm using regression trees. This is one of the first steps to building a dynamic pricing model.

Credit Card Fraud Detection as a Classification Problem
In this data science project, we will predict the credit card fraud in the transactional dataset using some of the predictive models.

Loan Eligibility Prediction using Gradient Boosting Classifier
This data science in python project predicts if a loan should be given to an applicant or not. We predict if the customer is eligible for loan based on several factors like credit score and past history.

Time Series Forecasting with LSTM Neural Network Python
Deep Learning Project- Learn to apply deep learning paradigm to forecast univariate time series data.

Solving Multiple Classification use cases Using H2O
In this project, we are going to talk about H2O and functionality in terms of building Machine Learning models.

Ecommerce product reviews - Pairwise ranking and sentiment analysis
This project analyzes a dataset containing ecommerce product reviews. The goal is to use machine learning models to perform sentiment analysis on product reviews and rank them based on relevance. Reviews play a key role in product recommendation systems.

Customer Churn Prediction Analysis using Ensemble Techniques
In this machine learning churn project, we implement a churn prediction model in python using ensemble techniques.

Data Science Project in Python on BigMart Sales Prediction
The goal of this data science project is to build a predictive model and find out the sales of each product at a given Big Mart store.

PySpark Tutorial - Learn to use Apache Spark with Python
PySpark Project-Get a handle on using Python with Spark through this hands-on data processing spark python tutorial.