কম্পিউটার

Python Pandas - DateTimeIndex-এ সময়কে মধ্যরাতে রূপান্তর করুন


DateTimeIndex-এ সময়কে মধ্যরাতে রূপান্তর করতে, DateTimeIndex.normalize() ব্যবহার করুন পান্ডায়।

প্রথমে, প্রয়োজনীয় লাইব্রেরিগুলি আমদানি করুন -

import pandas as pd

পিরিয়ড 7 এবং H অর্থাৎ ঘন্টা −

হিসাবে ফ্রিকোয়েন্সি সহ একটি DatetimeIndex তৈরি করুন
datetimeindex = pd.date_range('2021-10-30 02:30:50', periods=7, tz='Australia/Adelaide', freq='10H')

DateTimeIndex-

প্রদর্শন করুন
print("DateTimeIndex...\n", datetimeindex)

তারিখ-সময়ের সময় উপাদান মধ্যরাতে রূপান্তরিত হয় অর্থাৎ 00:00:00 −

print("\nNormalize (converted the time component to midnight)...\n",
datetimeindex.normalize())

উদাহরণ

নিম্নলিখিত কোড -

import pandas as pd

# DatetimeIndex with period 7 and frequency as H i.e. hours
# The timezone is Australia/Adelaide
datetimeindex = pd.date_range('2021-10-30 02:30:50', periods=7, tz='Australia/Adelaide', freq='10H')

# display DateTimeIndex
print("DateTimeIndex...\n", datetimeindex)

# display DateTimeIndex frequency
print("\nDateTimeIndex frequency...\n", datetimeindex.freq)

# The time component of the date-time is converted to midnight i.e. 00:00:00
print("\nNormalize (converted the time component to midnight)...\n",
datetimeindex.normalize())

আউটপুট

এটি নিম্নলিখিত কোড তৈরি করবে -

DateTimeIndex...
DatetimeIndex(['2021-10-30 02:30:50+10:30', '2021-10-30 12:30:50+10:30',
'2021-10-30 22:30:50+10:30', '2021-10-31 08:30:50+10:30',
'2021-10-31 18:30:50+10:30', '2021-11-01 04:30:50+10:30',
'2021-11-01 14:30:50+10:30'],
dtype='datetime64[ns, Australia/Adelaide]', freq='10H')
DateTimeIndex frequency...
<10 * Hours>

Normalize (converted the time component to midnight)...
DatetimeIndex(['2021-10-30 00:00:00+10:30', '2021-10-30 00:00:00+10:30',
'2021-10-30 00:00:00+10:30', '2021-10-31 00:00:00+10:30',
'2021-10-31 00:00:00+10:30', '2021-11-01 00:00:00+10:30',
'2021-11-01 00:00:00+10:30'],
dtype='datetime64[ns, Australia/Adelaide]', freq=None)

  1. পাইথন - একটি পান্ডাস ডেটাফ্রেমে একটি ডেটাটাইপকে অন্যটিতে রূপান্তর করুন

  2. Python Pandas - নেস্টেড ডিকশনারীকে মাল্টিইনডেক্স ডেটাফ্রেমে রূপান্তর করুন

  3. কিভাবে একটি স্প্রেডশীট পাইথন অভিধানে রূপান্তর করবেন?

  4. কিভাবে পান্ডা অফসেটকে পাইথন তারিখে রূপান্তর করবেন?