Pandas: Create a series with a PeriodIndex which represents all the calendar month periods in 2029 and 2031
Write a Pandas program create a series with a PeriodIndex which represents all the calendar month periods in 2029 and 2031. Also print the values for all periods in 2030.
Note: PeriodIndex is an immutable ndarray holding ordinal values indicating regular periods in time such as particular years, quarters, months, etc.
Sample Solution:
Python Code :
import pandas as pd
import numpy as np
pi = pd.Series(np.random.randn(36),
pd.period_range('1/1/2029',
'12/31/2031', freq='M'))
print("PeriodIndex which represents all the calendar month periods in 2029 and 2030:")
print(pi)
print("\nValues for all periods in 2030:")
print(pi['2030'])
Sample Output:
PeriodIndex which represents all the calendar month periods in 2029 and 2030: 2029-01 0.382450 2029-02 -1.077418 2029-03 0.131711 2029-04 1.044379 2029-05 -0.101383 2029-06 0.149212 2029-07 1.241014 2029-08 0.242632 2029-09 -0.252832 2029-10 -2.267629 2029-11 0.716542 2029-12 -1.397595 2030-01 0.005339 2030-02 0.454597 2030-03 1.140378 2030-04 1.024139 2030-05 1.744527 2030-06 0.827034 2030-07 -0.610342 2030-08 0.533601 2030-09 -2.200672 2030-10 -0.373814 2030-11 -1.366233 2030-12 0.173628 2031-01 0.691684 2031-02 -0.583149 2031-03 1.076706 2031-04 -3.059091 2031-05 -1.584180 2031-06 0.209410 2031-07 -0.984389 2031-08 -0.134217 2031-09 1.541736 2031-10 0.125816 2031-11 -0.066537 2031-12 -1.076096 Freq: M, dtype: float64 Values for all periods in 2030: 2030-01 0.005339 2030-02 0.454597 2030-03 1.140378 2030-04 1.024139 2030-05 1.744527 2030-06 0.827034 2030-07 -0.610342 2030-08 0.533601 2030-09 -2.200672 2030-10 -0.373814 2030-11 -1.366233 2030-12 0.173628 Freq: M, dtype: float64
Python Code Editor:
//------------- this portion above included on 07-02-2025 ------------------------------ var new_txt = 'Based on '+total_submit+' votes, average difficulty level of this exercise is '+difficulty+'.'; //'. '+difficulty+'/3'; var txt_node = document.createTextNode(new_txt); var level_result = document.getElementById('level_result'); level_result.appendChild(txt_node); } } else { alert('There was a problem with the request.'); } } } } function insert_level(event) { event.preventDefault(); var path = window.location; var page = path.href; var page = page.split('?'); var page = page[0]; //console.log(page); //console.log(page); /*var btns = document.getElementsByClassName("mdl-button mdl-js-button mdl-button--raised mdl-button--colored"); for (var i = 0; i < btns.length; i++) { var clicked = btns[i].id; }*/ var clicked = this.id; if(clicked=="easy") clicked=1; if(clicked=="medium") clicked=2; if(clicked=="hard") clicked=3; console.log(clicked); var httpRequest1 = new XMLHttpRequest(); if (!httpRequest1) { alert('Giving up :( Cannot create an XMLHTTP instance'); //return false; } var url = "/assets/level_insert.php"; var data1 = "level=" + clicked + "&page=" + page; httpRequest1.onreadystatechange = displayContent1; httpRequest1.open("POST", url, true); httpRequest1.setRequestHeader("Content-Type", "application/x-www-form-urlencoded"); httpRequest1.send(data1); console.log(data1); //console.log("found"); function displayContent1(responseText) { if (httpRequest1.readyState === XMLHttpRequest.DONE) { if (httpRequest1.status === 200) { var op = httpRequest1.responseText; console.log(op); } else { alert('There was a problem with the request.'); } } } } var easy = document.getElementById("easy"); easy.addEventListener('click', insert_level, false); var medium = document.getElementById("medium"); medium.addEventListener('click', insert_level, false); var hard = document.getElementById("hard"); hard.addEventListener('click', insert_level, false);Test your Programming skills with w3resource's quiz.
- Weekly Trends and Language Statistics
- Weekly Trends and Language Statistics