Computer >> कंप्यूटर ट्यूटोरियल >  >> प्रोग्रामिंग >> Python

पायथन पैंडास - पीरियड ऑब्जेक्ट से वर्ष का दिन प्राप्त करें


पीरियड ऑब्जेक्ट से वर्ष का दिन प्राप्त करने के लिए, period.dayofyear प्रॉपर्टी का उपयोग करें।

सबसे पहले, आवश्यक लाइब्रेरी इंपोर्ट करें:

import pandas as pd

pandas.Period समय की अवधि को दर्शाता है। दो पीरियड ऑब्जेक्ट बनाएं:

period1 = pd.Period("2020-09-23")
period2 = pd.Period(freq="D", year=2021, month=7, day=16, hour=2, minute=35)

पीरियड ऑब्जेक्ट्स प्रदर्शित करें:

print("Period1...\n", period1)
print("Period2...\n", period2)

दो पीरियड ऑब्जेक्ट्स से वर्ष का दिन प्राप्त करें:

res1 = period1.dayofyear
res2 = period2.dayofyear

उदाहरण

import pandas as pd
# The pandas.Period represents a period of time
# creating two Period objects
period1 = pd.Period("2020-09-23")
period2 = pd.Period(freq="D", year=2021, month=7, day=16, hour=2, minute=35)
# display the Period objects
print("Period1...\n", period1)
print("Period2...\n", period2)
# get the day of the year from two Period objects
res1 = period1.dayofyear
res2 = period2.dayofyear
# Return the day of the year from the two Period objects
# The return value ranges between 1 to 365 for regular years and 1 to 366 for leap years
print("\nDay of the year from the 1st Period object ...\n", res1)
print("\nDay of the year from the 2nd Period object...\n", res2)

आउटपुट

Period1... 2020-09-23
Period2... 2021-07-16
Day of the year from the 1st Period object ... 267
Day of the year from the 2nd Period object... 197
  1. - पायथन पैंडास - टाइमडेल्टा ऑब्जेक्ट से अवधि के कुल सेकंड प्राप्त करें

    टाइमडेल्टा ऑब्जेक्ट से अवधि के कुल सेकंड प्राप्त करने के लिए, timedelta.total_seconds() मेथड का उपयोग करें। सबसे पहले, आवश्यक लाइब्रेरी इंपोर्ट करें: import pandas as pd TimeDeltas पायथन के स्टैंडर्ड डेटाइम लाइब्रेरी का उपयोग करता है timedeltas के लिए अलग प्रतिनिधित्व, एक टाइमडेल्टा ऑब्जेक्ट बनाएं:

  1. - पायथन - पैंडास में टाइमस्टैम्प ऑब्जेक्ट से वीकडे प्राप्त करें

    टाइमस्टैम्प ऑब्जेक्ट से वीकडे प्राप्त करने के लिए timestamp.weekday() मेथड का उपयोग करें। सबसे पहले आवश्यक लाइब्रेरी इम्पोर्ट करें: import pandas as pd import datetime पैंडास में टाइमस्टैम्प सेट करें। टाइमस्टैम्प ऑब्जेक्ट बनाएं: timestamp = pd.Timestamp(datetime.datetime(2021, 5, 12)) वर्ष का वीकडे

  1. - पायथन में वर्ष का दिन

    मान लीजिए, हमारे पास YYYY-MM-DD फॉर्मेट में एक तारीख है। हमें वर्ष का दिन नंबर लौटाना है। इसलिए यदि तारीख 2019-02-10 है, तो यह वर्ष का 41वां दिन है। समाधान के चरण मान लीजिए D दिनों की गिनती का एक ऐरे है जैसे [0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31] तारीख को वर्ष, महीना और दिन की सूची मे