Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Server Side Programming Articles
Page 243 of 2109
Program to find length of the longest path in an n-ary tree in Python
In this problem, we need to find the longest path in an n-ary tree represented by an edge list. Each edge (u, v) indicates that u is the parent of v. The path length is calculated as 1 + number of nodes in the path. For the given tree structure: 1 ...
Read MorePython Pandas - Return DatetimeIndex as object ndarray of datetime.datetime objects
In Pandas, a DatetimeIndex stores datetime values as NumPy datetime64 objects. Sometimes you need to convert these to native Python datetime.datetime objects. Use the to_pydatetime() method to return a DatetimeIndex as an object ndarray of datetime.datetime objects. Syntax DatetimeIndex.to_pydatetime() This method returns an object ndarray where each element is a Python datetime.datetime object. Creating a DatetimeIndex First, let's create a DatetimeIndex with a specific frequency ? import pandas as pd # Create DatetimeIndex with period 5 and frequency as 2 years datetimeindex = pd.date_range('2021-10-18 07:20:32.261811624', periods=5, freq='2Y') print("DateTimeIndex...") print(datetimeindex) ...
Read MorePython Pandas - Calculate TimedeltaArray of difference between index values and index converted to PeriodArray at specified freq
To calculate TimedeltaArray of difference between index values and index converted to PeriodArray at specified frequency, use the to_perioddelta() method on a DatetimeIndex. Set the frequency using the freq parameter. What is to_perioddelta()? The to_perioddelta() method calculates the time difference between each datetime value and the start of its corresponding period. For example, with monthly frequency ('M'), it shows how far into each month each datetime falls. Creating a DatetimeIndex First, create a DatetimeIndex with specific periods and frequency ? import pandas as pd # Create DatetimeIndex with 5 periods, every 2 years ...
Read MorePython Pandas - How to convert DateTimeIndex to Period
To convert DateTimeIndex to Period, use the datetimeindex.to_period() method in Pandas. The frequency is set using the freq parameter. Creating a DateTimeIndex First, let's create a DateTimeIndex with specific period and frequency ? import pandas as pd # Create DatetimeIndex with period 5 and frequency as 2 years datetimeindex = pd.date_range('2021-10-18 07:20:32.261811624', periods=5, freq='2Y') print("DateTimeIndex...") print(datetimeindex) print("DateTimeIndex frequency...") print(datetimeindex.freq) DateTimeIndex... DatetimeIndex(['2021-12-31 07:20:32.261811624', '2023-12-31 07:20:32.261811624', ...
Read MoreProgram to find area of largest submatrix by column rearrangements in Python
Suppose we have a binary matrix. We can rearrange the columns as many times as we want, then find the area of the largest submatrix containing only 1s. So, if the input is like ? 1 0 0 ...
Read MorePython Pandas - How to perform ceil operation on the DateTimeIndex with specified frequency
To perform ceil operation on the DateTimeIndex with specified frequency, use the DateTimeIndex.ceil() method. The freq parameter specifies the frequency to which each timestamp should be rounded up. What is Ceil Operation? The ceil() method rounds timestamps up to the nearest specified frequency unit. For example, if you ceil to microseconds ('us'), any nanosecond precision will be rounded up to the next microsecond. Syntax DateTimeIndex.ceil(freq) Parameters freq: String representing the frequency to ceil to (e.g., 'S' for seconds, 'us' for microseconds, 'H' for hours) Example Let's create a ...
Read MoreProgram to find largest kth index value of one list in Python
Given three values n, total, and k, we need to find the maximum value at index k in a list of size n. The list must satisfy two conditions: its sum equals total, and the absolute difference between consecutive elements is at most 1. So, if the input is like n = 5, total = 15, k = 3, then the output will be 4, because one possible list is [3, 2, 3, 4, 3], where the maximum element at index 3 is 4. Algorithm Steps To solve this, we will follow these steps − ...
Read MorePython Pandas - How to perform ceil operation on the DateTimeIndex with microseconds frequency
To perform ceil operation on the DateTimeIndex with microseconds frequency, use the DateTimeIndex.ceil() method. The ceil operation rounds up datetime values to the next higher boundary. For microseconds frequency, use the freq parameter with value 'us'. What is the Ceil Operation? The ceil operation rounds datetime values upward to the nearest specified frequency boundary. When applied with microseconds frequency ('us'), it rounds up to the next microsecond. Creating a DateTimeIndex First, let's create a DateTimeIndex with nanosecond precision ? import pandas as pd # Create DatetimeIndex with period 5 and frequency as 40 ...
Read MorePython Pandas - How to perform ceil operation on the DateTimeIndex with milliseconds frequency
To perform a ceil operation on the DateTimeIndex with milliseconds frequency, use the DateTimeIndex.ceil() method. For milliseconds frequency, use the freq parameter with value 'ms'. What is the Ceil Operation? The ceil operation rounds timestamps up to the nearest specified frequency. For milliseconds, it rounds up to the next millisecond boundary, removing microseconds and nanoseconds precision. Syntax DateTimeIndex.ceil(freq) Parameters freq − The frequency to round up to. Use 'ms' for milliseconds Example Let's create a DateTimeIndex with microsecond precision and apply the ceil operation ? import ...
Read MoreProgram to find kth smallest element in linear time in Python
Finding the kth smallest element in a list is a common problem that can be solved efficiently using heap data structures. The challenge is to achieve O(n) average time complexity rather than the naive O(n log n) sorting approach. So, if the input is like nums = [6, 4, 9, 3, 1] and k = 2, then the output will be 4. After sorting, the list becomes [1, 3, 4, 6, 9], where the 2nd smallest element (0-indexed) is 4. Algorithm Steps To solve this efficiently, we will follow these steps − Create a max ...
Read More