DateTimeIndex में टाइम स्टैम्प्स को निकटतम घटित आवृत्ति पर स्नैप करने के लिए DateTimeIndex.snap() मेथड का उपयोग करें। freq पैरामीटर का उपयोग करके आवृत्ति सेट करें।
आवश्यक लाइब्रेरी आयात करें
import pandas as pd
DateTimeIndex बनाएं
पीरियड 6 और आवृत्ति D यानी दिन के साथ DatetimeIndex बनाएं।
datetimeindex = pd.date_range('2021-10-20 02:30:50', periods=6, tz='Australia/Adelaide', freq='D')
DateTimeIndex प्रदर्शित करें
print("DateTimeIndex...\n", datetimeindex)
टाइम स्टैम्प्स को निकटतम घटित यानी महीने के अंत पर स्नैप करें
print("\nSnap time stamps to nearest occurring frequency...\n", datetimeindex.snap(freq='M'))
पूरा उदाहरण
import pandas as pd
# DatetimeIndex with period 6 and frequency as D i.e. day
# The timezone is Australia/Adelaide
datetimeindex = pd.date_range('2021-10-20 02:30:50', periods=6, tz='Australia/Adelaide', freq='D')
# display DateTimeIndex
print("DateTimeIndex...\n", datetimeindex)
# display DateTimeIndex frequency
print("\nDateTimeIndex frequency...\n", datetimeindex.freq)
# Snap time stamps to nearest occurring i.e. Month end here
print("\nSnap time stamps to nearest occurring frequency...\n", datetimeindex.snap(freq='M'))
आउटपुट
DateTimeIndex... DatetimeIndex(['2021-10-20 02:30:50+10:30', '2021-10-21 02:30:50+10:30', '2021-10-22 02:30:50+10:30', '2021-10-23 02:30:50+10:30', '2021-10-24 02:30:50+10:30', '2021-10-25 02:30:50+10:30'], dtype='datetime64[ns, Australia/Adelaide]', freq='D') DateTimeIndex frequency...Snap time stamps to nearest occurring frequency... DatetimeIndex(['2021-10-31 02:30:50+10:30', '2021-10-31 02:30:50+10:30', '2021-10-31 02:30:50+10:30', '2021-10-31 02:30:50+10:30', '2021-10-31 02:30:50+10:30', '2021-10-31 02:30:50+10:30'], dtype='datetime64[ns, Australia/Adelaide]', freq=None)