ARunningFridge
ARunningFridge

Reputation: 49

How to change pandas' Datetime Index from "End of month" To just "Month"

I'm using pandas to analyze some data about the House Price Index of all states from quandl: HPI_Data = quandl.get("FMAC/HPI_AK")

The data looks something like this:

              HPI Alaska
Date
1975-01-31    35.105461 
1975-02-28    35.465209
1975-03-31    35.843110

and so on.

I've got a second dataframe with some special dates in it:

          Date
Name
David    1979-08
Allen    1980-08
Hugo     1989-09

The values for "Date" here are of "string" type and not "date". I'd like to go 6 months back from each date in the special dataframe and see the values in the HPI dataframe. I'd like to use .loc but I have not been able to convert the first dataframe's index from "END OF MONTH" to "MONTH". even after resampling to "1D" then back to "M".

I'd would appreciate any help, if it solves the problem a different way or the janky data deleting way I want :).

Upvotes: 1

Views: 1231

Answers (1)

bkeesey
bkeesey

Reputation: 496

Not sure if I understand correctly. So please clarify your question if this is not correct.

You can convert a string to a pandas date time object using pd.to_datetime and use the format parameter to specify how to parse the string

import pandas as pd

# Creating a dummy Series
sr = pd.Series(['2012-10-21 09:30', '2019-7-18 12:30', '2008-02-2 10:30',
                '2010-4-22 09:25', '2019-11-8 02:22'])

# Convert the underlying data to datetime 
sr = pd.to_datetime(sr)

# Subtract 6 months of the datetime series
sr-pd.DateOffset(month=6)

In regards to changing the datetime to just month i.e. 2012-10-21 09:30 --> 2012-10 I would do this:

sr.dt.to_period('M')

Upvotes: 1

Related Questions