Python Pandas - Return the frequency applied on the given DateOffset object

PythonServer Side ProgrammingProgramming

<p>To return the frequency applied on the given DateOffset object, use the offset.freqstr in Pandas. At first, import the required libraries &minus;</p><pre class="just-code notranslate language-python3" data-lang="python3">from pandas.tseries.frequencies import to_offset 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-09-26 03:25:02.000045&#39;) </pre><p>Create the DateOffset. We are incrementing the days here using the &quot;D&quot; frequency &minus;</p><pre class="just-code notranslate language-python3" data-lang="python3">offset = to_offset(&quot;5D&quot;)</pre><p>Display the Updated Timestamp &minus;</p><pre class="just-code notranslate language-python3" data-lang="python3">print(&quot; Updated Timestamp... &quot;,timestamp + offset) </pre><p>Return the frequency applied on the given DateOffset object &minus;</p><pre class="just-code notranslate language-python3" data-lang="python3">print(&quot; The frequency on the DateOffset object.. &quot;, offset.freqstr)</pre><h2>Example</h2><p>Following is the code &minus;</p><pre class="demo-code notranslate language-python3" data-lang="python3">from pandas.tseries.frequencies import to_offset import pandas as pd # Set the timestamp object in Pandas timestamp = pd.Timestamp(&#39;2021-09-26 03:25:02.000045&#39;) # Display the Timestamp print(&quot;Timestamp... &quot;,timestamp) # Create the DateOffset # We are incrementing the days here using the &quot;D&quot; frequency offset = to_offset(&quot;5D&quot;) # Display the DateOffset print(&quot; DateOffset... &quot;,offset) # Display the Updated Timestamp print(&quot; Updated Timestamp... &quot;,timestamp + offset) # return the frequency applied on the given DateOffset object print(&quot; The frequency on the DateOffset object.. &quot;, offset.freqstr)</pre><h2>Output</h2><p>This will produce the following code &minus;</p><pre class="result notranslate">Timestamp... 2021-09-26 03:25:02.000045 DateOffset... &lt;5 * Days&gt; Updated Timestamp... 2021-10-01 03:25:02.000045 The frequency on the DateOffset object.. 5D</pre>
raja
Updated on 21-Oct-2021 07:50:28

Advertisements