Python Pandas - Get the weekmask applied on the CustomBusinessHour offset

PythonServer Side ProgrammingProgramming

<p>To get the weekmask applied on the CustomBusinessHour offset, use the CustomBusinessHour.weekmask property in Pandas.</p><p>At first, import the required libraries &minus;</p><pre class="just-code notranslate language-python3" data-lang="python3">import pandas as pd</pre><p>Set the timestamp object in Pandas &minus;</p><pre class="just-code notranslate language-python3" data-lang="python3">timestamp = pd.Timestamp(&#39;2021-11-14 05:20:30&#39;) </pre><p>Create the CustomBusinessHour Offset. CustomBusinessHour is the DateOffset subclass. Weekmask of valid business days &minus;</p><pre class="just-code notranslate language-python3" data-lang="python3">cbhOffset = pd.tseries.offsets.CustomBusinessHour(n = 7, weekmask = &#39;Mon Tue Wed Fri&#39;)</pre><p>Add the offset to the Timestamp and display the Updated Timestamp &minus;</p><pre class="just-code notranslate language-python3" data-lang="python3">print(&quot; Updated Timestamp... &quot;,timestamp + cbhOffset) </pre><p>Display the weekmask &minus;</p><pre class="just-code notranslate language-python3" data-lang="python3">print(&quot; The weekmask on the CustomBusinessHour object.. &quot;, cbhOffset.weekmask)</pre><h2>Example</h2><p>Following is the code &minus;</p><pre class="demo-code notranslate language-python3" data-lang="python3">import pandas as pd # Set the timestamp object in Pandas timestamp = pd.Timestamp(&#39;2021-11-14 05:20:30&#39;) # Display the Timestamp print(&quot;Timestamp... &quot;,timestamp) # Create the CustomBusinessHour Offset # CustomBusinessHour is the DateOffset subclass # Weekmask of valid business days cbhOffset = pd.tseries.offsets.CustomBusinessHour(n = 7, weekmask = &#39;Mon Tue Wed Fri&#39;) # Display the CustomBusinessHour Offset print(&quot; CustomBusinessHour Offset... &quot;,cbhOffset) # Add the offset to the Timestamp and display the Updated Timestamp print(&quot; Updated Timestamp... &quot;,timestamp + cbhOffset) # Return frequency applied on the given CustomBusinessHour Offset object as a string print(&quot; Frequency applied on the given CustomBusinessHour Offset object... &quot;,cbhOffset.freqstr) # display the weekmask print(&quot; The weekmask on the CustomBusinessHour object.. &quot;, cbhOffset.weekmask)</pre><h2>Output</h2><p>This will produce the following code &minus;</p><pre class="result notranslate">Timestamp... 2021-11-14 05:20:30 CustomBusinessHour Offset... &lt;7 * CustomBusinessHours: CBH=09:00-17:00&gt; Updated Timestamp... 2021-11-15 16:00:00 Frequency applied on the given CustomBusinessHour Offset object... 7CBH The weekmask on the CustomBusinessHour object.. Mon Tue Wed Fri</pre>
raja
Updated on 22-Oct-2021 08:02:27

Advertisements