Python | Pandas Series.dt.is_leap_year

| | |

👻 Check our latest review to choose the best laptop for Machine Learning engineers and Deep learning tasks!

Series.dt can be used to access series values ‚Äã‚Äãas datetimelike and return multiple properties. Series.dt.is_leap_year Pandas Series.dt.is_leap_year returns a boolean indicator if the date is in a leap year.

Syntax: Series.dt.is_leap_year

Parameter: None

Returns: numpy array

Example # 1: Use the Series.dt.is_leap_year attribute to check if dates in the underlying data of this series object by leap year.

# import pandas as pd

import pandas as pd


# Create series

sr = pd.Series ([ ’ 2012-12-31’ , ’2019-1-1 12: 30’ , ’2008-02-2 10:30’ ,

’2010-1-1 09:25’ , ’2019-12-31 00:00’ ])


# Create index

idx = [ ’Day 1’ , ’Day 2’ , ’ Day 3’ , ’Day 4’ , ’ Day 5 ’ ]


#install search index

sr.index = idx


# Convert baseline data to date and time

sr = pd.to_datetime (sr)


# Print series

print (sr)

Output:

We will now use the Series.dt.is_leap_year attribute to check if the dates in this series object are in a leap year.

#check, e If dates are given
# belongs to a leap year.

result = sr.dt.is_leap_year


# print the result

print (result)

Output:

As we can see from the output, the attribute is Series.dt.is_leap_year successfully accessed and returned a Boolean indicating whether the dates in this series object are in a leap year.

Example # 2: Use the Series.dt.is_leap_year attribute to check if the dates in the underlying data for this series object are in a leap year.

# import pandas as pd

import pandas as pd


# Create a series

sr = pd.Series (pd.date_range ( ’2012-12-31 00:00’ , periods = 5 , freq = ’D’ ))


# Create index

idx = [ ’Day 1’ , ’Day 2’ , ’Day 3’ , ’ Day 4’ , ’ Day 5’ ]


# set index

sr.index = idx


# Print series

print (sr)

Output:

We will now use the Series.dt.is_leap_year attribute to check if the dates in the given object of the series are in a leap year.

# check if dates are given
# belongs to leap year.

result = sr.dt.is_leap_year


# print the result

print (result)

Output:

As we can see from the output, the attribute Series.dt.is_leap_year successfully accessed and returned Boolean values ‚Äã‚Äãindicating whether the dates in this series object are in a leap year.

👻 Read also: what is the best laptop for engineering students?

Python | Pandas Series.dt.is_leap_year date_range: Questions

How do I filter query objects by date range in Django?

2 answers

I"ve got a field in one model like:

class Sample(models.Model):
    date = fields.DateField(auto_now=False)

Now, I need to filter the objects by a date range.

How do I filter all the objects that have a date between 1-Jan-2011 and 31-Jan-2011?

299

Answer #1

Use

Sample.objects.filter(date__range=["2011-01-01", "2011-01-31"])

Or if you are just trying to filter month wise:

Sample.objects.filter(date__year="2011", 
                      date__month="01")

Edit

As Bernhard Vallant said, if you want a queryset which excludes the specified range ends you should consider his solution, which utilizes gt/lt (greater-than/less-than).

299

Answer #2

You can use django"s filter with datetime.date objects:

import datetime
samples = Sample.objects.filter(sampledate__gte=datetime.date(2011, 1, 1),
                                sampledate__lte=datetime.date(2011, 1, 31))

We hope this article has helped you to resolve the problem. Apart from Python | Pandas Series.dt.is_leap_year, check other date_range-related topics.

Want to excel in Python? See our review of the best Python online courses 2022. If you are interested in Data Science, check also how to learn programming in R.

By the way, this material is also available in other languages:



Dmitry Sikorski

Abu Dhabi | 2022-11-28

open is always a bit confusing 😭 Python | Pandas Series.dt.is_leap_year is not the only problem I encountered. Will use it in my bachelor thesis

Frank Robinson

California | 2022-11-28

Thanks for explaining! I was stuck with Python | Pandas Series.dt.is_leap_year for some hours, finally got it done 🤗. I just hope that will not emerge anymore

Davies Emmerson

California | 2022-11-28

I was preparing for my coding interview, thanks for clarifying this - Python | Pandas Series.dt.is_leap_year in Python is not the simplest one. I am just not quite sure it is the best method

Shop

Learn programming in R: courses

$

Best Python online courses for 2022

$

Best laptop for Fortnite

$

Best laptop for Excel

$

Best laptop for Solidworks

$

Best laptop for Roblox

$

Best computer for crypto mining

$

Best laptop for Sims 4

$

Latest questions

NUMPYNUMPY

Common xlabel/ylabel for matplotlib subplots

12 answers

NUMPYNUMPY

How to specify multiple return types using type-hints

12 answers

NUMPYNUMPY

Why do I get "Pickle - EOFError: Ran out of input" reading an empty file?

12 answers

NUMPYNUMPY

Flake8: Ignore specific warning for entire file

12 answers

NUMPYNUMPY

glob exclude pattern

12 answers

NUMPYNUMPY

How to avoid HTTP error 429 (Too Many Requests) python

12 answers

NUMPYNUMPY

Python CSV error: line contains NULL byte

12 answers

NUMPYNUMPY

csv.Error: iterator should return strings, not bytes

12 answers

News


Wiki

Python | How to copy data from one Excel sheet to another

Common xlabel/ylabel for matplotlib subplots

Check if one list is a subset of another in Python

sin

How to specify multiple return types using type-hints

exp

Printing words vertically in Python

exp

Python Extract words from a given string

Cyclic redundancy check in Python

Finding mean, median, mode in Python without libraries

cos

Python add suffix / add prefix to strings in a list

Why do I get "Pickle - EOFError: Ran out of input" reading an empty file?

Python - Move item to the end of the list

Python - Print list vertically